socket.handler.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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("pc_doctor"===data.clientType){//新增pc端医生登录
  30. data.userId = "pc_"+data.userId;
  31. }
  32. clientCache.removeByUserId(data.userId);
  33. // if(clientCache.removeByUserId(data.userId)){
  34. // log.info("User " + data.userId + " already login");
  35. // return;
  36. // }
  37. log.error('User ' + data.userId + ' login');
  38. if(!data.clientType||data.clientType=="patient"){
  39. let patientClient = new PatientClient(socket, socketServer);
  40. patientClient.userId = data.userId;
  41. patientClient.password = data.password;
  42. patientClient.clientType = data.clientType||"patient";
  43. patientClient.sessionId = data.sessionId||"";
  44. clientCache.addClient(patientClient);
  45. users.login(data.userId, 10, '', '');
  46. socket.emit('ack', {});
  47. }else{
  48. let doctorClient = new DoctorClient(socket, socketServer);
  49. doctorClient.userId = data.userId;
  50. doctorClient.password = data.password;
  51. doctorClient.clientType = data.clientType;
  52. doctorClient.sessionId = data.sessionId||"";
  53. clientCache.addClient(doctorClient);
  54. socket.emit('ack', {});
  55. }
  56. }
  57. });
  58. // 接收客户端消息
  59. socket.on('message', function (data) {
  60. log.info('Got message from ' + clientCache.findBySocket(socket).userId);
  61. let sessionId = data.session_id;
  62. let message = data.message;
  63. message.timestamp = new Date();
  64. /*sessions.createSession(sessionId, "Let's talk!", 1, ['504a0bcddb1e4e37a0306b39c51900b5', 'cd92414c-5b06-11e6-8344-fa163e8aee56'], function (err, res) {
  65. sessions.saveMessageBySession(sessionId, message);
  66. });*/
  67. sessions.saveMessageBySession(sessionId, message);
  68. });
  69. // 客户端退出
  70. socket.on('logout', function (data) {
  71. let client = clientCache.findBySocket(socket);
  72. if (client) {
  73. log.info('User logout: ' + client.userId);
  74. clientCache.removeByUserId(client.userId);
  75. }
  76. });
  77. // 客户端断开
  78. socket.on('disconnect', function () {
  79. let patientClient = clientCache.findBySocket(socket);
  80. if (patientClient) {
  81. log.info("User disconnect: ", patientClient.userId);
  82. clientCache.removeByUserSocket(socket);
  83. }
  84. });
  85. socket.emit('welcome', {message: 'Welcome to connect IM server, please login.'});
  86. });
  87. }
  88. }
  89. module.exports = SocketHandler;