socket.handler.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * author: Sand
  3. * since: 2016/11/17
  4. */
  5. "use strict";
  6. let log = require("../util/log.js");
  7. let clientCache = require('../models/socket.io/client.cache').clientCache();
  8. let PatientClient = require('./../models/socket.io/patient.client');
  9. let DoctorClient = require('./../models/socket.io/doctor.client');
  10. let Sessions = require('../models/sessions/sessions');
  11. let Users = require('../models/user/users');
  12. let sessions = new Sessions();
  13. let users = new Users();
  14. class SocketHandler {
  15. constructor(socketServer) {
  16. this._socketServer = socketServer;
  17. }
  18. /**
  19. * 启用事件监听。
  20. */
  21. start() {
  22. let socketServer = this._socketServer;
  23. socketServer.sockets.on('connection', function (socket) {
  24. // 客户端注册
  25. socket.on('login', function (data) {
  26. if (!data.userId) {
  27. socketServer.sockets.emit('error', {message: 'Missing fields(s): userId.'});
  28. } else {
  29. if(clientCache.removeByUserId(data.userId)){
  30. log.info("User " + data.userId + " already login");
  31. return;
  32. }
  33. log.info('User ' + data.userId + ' login');
  34. if(!data.clientType||data.clientType=="patient"){
  35. let patientClient = new PatientClient(socket, socketServer);
  36. patientClient.userId = data.userId;
  37. patientClient.password = data.password;
  38. patientClient.clientType = data.clientType||"patient";
  39. clientCache.addClient(patientClient);
  40. users.login(data.userId, 10, '', '');
  41. socket.emit('ack', {});
  42. }else{
  43. let doctorClient = new DoctorClient(socket, socketServer);
  44. doctorClient.userId = data.userId;
  45. doctorClient.password = data.password;
  46. doctorClient.clientType = data.clientType;
  47. doctorClient.sessionId = data.sessionId;
  48. clientCache.addClient(doctorClient);
  49. socket.emit('ack', {});
  50. }
  51. }
  52. });
  53. // 接收客户端消息
  54. socket.on('message', function (data) {
  55. log.info('Got message from ' + clientCache.findBySocket(socket).userId);
  56. let sessionId = data.session_id;
  57. let message = data.message;
  58. message.timestamp = new Date();
  59. /*sessions.createSession(sessionId, "Let's talk!", 1, ['504a0bcddb1e4e37a0306b39c51900b5', 'cd92414c-5b06-11e6-8344-fa163e8aee56'], function (err, res) {
  60. sessions.saveMessageBySession(sessionId, message);
  61. });*/
  62. sessions.saveMessageBySession(sessionId, message);
  63. });
  64. // 客户端退出
  65. socket.on('logout', function (data) {
  66. let client = clientCache.findBySocket(socket);
  67. if (client) {
  68. log.info('User logout: ' + client.userId);
  69. clientCache.removeByUserId(client.userId);
  70. }
  71. });
  72. // 客户端断开
  73. socket.on('disconnect', function () {
  74. let patientClient = clientCache.findBySocket(socket);
  75. if (patientClient) {
  76. log.info("User disconnect: ", patientClient.userId);
  77. clientCache.removeByUserSocket(socket);
  78. }
  79. });
  80. socket.emit('welcome', {message: 'Welcome to connect IM server, please login.'});
  81. });
  82. }
  83. }
  84. module.exports = SocketHandler;