imCommon.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. var F = require('../common/function');
  3. var C = require('../config/index');
  4. var _ = require('underscore');
  5. _.str = require('underscore.string');
  6. _.v = require('validator');
  7. var co = require('co');
  8. module.exports = function (app){
  9. var model_map = app.model_mgr.model_map;
  10. var mgr_map = app.common_mgr.mgr_map;
  11. var port = C.port + 1;
  12. var back_svr = _.str.sprintf("http://%s:%s", C.inner_host, port);
  13. var imapp = app;
  14. imapp.onconnect = function* (next, ctx) {
  15. // on connect
  16. var cur_time = new Date();
  17. var socket_id = ctx.socket.id;
  18. ctx.connect_time = cur_time.getTime();
  19. F.addLogs("im-svr " + socket_id + " receive connect suc");
  20. setTimeout(function () {
  21. if (F.isNull(ctx.uid)) ctx.disconnect();
  22. },10000); // 10秒后不登录则强制断开socket
  23. };
  24. imapp.ondisconnect = function* (next, ctx, reason) {
  25. // on disconnect
  26. var userid = ctx.userid;
  27. var uniid = ctx.uniid;
  28. var socket_id = ctx.socket.id;
  29. F.addDebugLogs(["socket disconnect:",uniid,socket_id,reason]);
  30. if (!F.isNull(ctx.uniid)) {
  31. yield mgr_map.im.delSvrMap(ctx.uniid,false); //注销im消息管理
  32. yield mgr_map.room.abnormalClose(ctx.uniid,ctx,ctx.page_name); //异常关闭处理
  33. }
  34. };
  35. imapp.beforeCallback = function* (route, ctx, message) {
  36. message.req_data = F.rmHtmlAndJsByJson(message.req_data);//进行html编码
  37. if(message.route != 'heartbeat') {
  38. F.addOtherLogs("imrw/imrw",['##im receive: uniid: ', ctx.uniid, 'userid: ', ctx.userid, message]);
  39. }
  40. if (!F.isNull(ctx.has_del)) { // 已经被删除
  41. if (route == "kickoff") { // 收到回复才真正删除
  42. ctx.disconnect();
  43. }
  44. return false;
  45. }
  46. var allow_route_list = ["login","heartbeat","test","test2"]; // 允许不登录访问的路由
  47. if (_.v.isIn(message.route,allow_route_list)) return true;
  48. if (F.isNull(ctx.uid)) F.throwErrCode(10002);
  49. return true;
  50. };
  51. imapp.catchException = function* (route,ctx,message,err) {
  52. var msg = JSON.stringify(message);
  53. var err_str = F.log("err:", "#####%s; req:%s; catch a err:%s,code:%s", [route,msg,err.message,err.code]);
  54. F.addErrLogs([err_str,err.stack]);
  55. var errno = F.isNull(err.code) || !F.isInt(err.code) ? 10003 : err.code;
  56. var res = {
  57. "errno": errno,
  58. "errmsg": F.isNull(err.use_msg) ? C.err_msg[errno.toString()] : err.message/*,
  59. "err_stack": err.stack*/
  60. }
  61. if(!F.isNull(err.data)) {
  62. res.data = err.data;
  63. res.errdata = err.data;
  64. }
  65. yield sendRes(ctx, message, res);
  66. };
  67. var sendRes = function* (ctx, message, res) {
  68. if(!F.isNull(ctx.uniid)) {
  69. yield mgr_map.im.sendRes(ctx.uniid, message, res);
  70. }else {
  71. let send_data = {
  72. 'id':message.id,
  73. 'route':message.route,
  74. 'res_data':res
  75. }
  76. imapp.emit(ctx, message.route, send_data);
  77. }
  78. }
  79. };