root 1 年之前
父节点
当前提交
23c116ba5d
共有 1 个文件被更改,包括 78 次插入46 次删除
  1. 78 46
      node/message.js

+ 78 - 46
node/message.js

@@ -192,14 +192,14 @@ us.server = us.http.createServer(function (requset, response) {
                 //执行登录函数
                 case 'login':
                     _userinfo = JSON.parse(_param[0]); //登录用户的信息
-                    us.login(response, _userinfo.userId, _userinfo.pageId, _userinfo.userName, _userinfo.type);
+                    us.login(requset, response, _userinfo.userId, _userinfo.pageId, _userinfo.userName, _userinfo.type);
                     break;
                 case 'logout':
                     us.logout(response, _param[0], _param[1]);
                     break;
                 //执行轮询函数
                 case 'polling':
-                    us.polling(response, _param[0], _param[1], _param[2], _param[3]);
+                    us.polling(requset, response, _param[0], _param[1], _param[2], _param[3]);
                     break;
                 //获取文档信息
                 case 'getWord':
@@ -417,8 +417,8 @@ us.sendUser = function (userinfo, type, pageid, fileid) {
         docId: fileid
     });
     _message[fileid] = [_messageinfo];
-    console.log("2222222222")
-    console.log(_message)
+    // console.log("2222222222")
+    // console.log(_message)
     us.broadcast(_message, pageid, type);
 }
 
@@ -466,15 +466,11 @@ us.unifyDispose = function (req, res, cb) {
     res.on('timeout', function (e) {
         try {
             console.log('请求超时'); //错误消息的输出
-            if (req.res) {
-                req.res("abort");
-            }
             res.end("false"); //返回值
         }
         catch (e) {
             console.error('process error is:', e.message);
         }
-
     });
 
     //设置30秒后超时
@@ -509,17 +505,18 @@ us.unifyDispose = function (req, res, cb) {
 * @param  {string} username 用户名
 * @param  {type} username 用户登录类型
 **/
-us.login = function (response, userid, pageid, username, type) {
-    //用户登录信息表,以后移入redis
-    //如果用户信息不存在则新建一个用户信息存入
-    if (!us.user[userid]) {
-        username = username || "";
-        console.log(username)
-        var _userinfo = us.userInfo(userid, username); //初始化用户信息
-        us.user[userid] = _userinfo;
+us.login = function (requset, response, userid, pageid, username, type) {
+    if (userid && pageid) {
+        //用户登录信息表,以后移入redis
+        //如果用户信息不存在则新建一个用户信息存入
+        if (!us.user[userid]) {
+            username = username || "";
+            var _userinfo = us.userInfo(userid, username); //初始化用户信息
+            us.user[userid] = _userinfo;
+        }
+        //添加用户轮询列表
+        us.polling(requset, response, userid, pageid, type);
     }
-    //添加用户轮询列表
-    us.polling(response, userid, pageid, type);
 }
 
 /**
@@ -529,27 +526,11 @@ us.login = function (response, userid, pageid, username, type) {
 * @param {string} pageid   页面ID(唯一识别ID)
 */
 us.offLine = function (response, userid, pageid) {
-    //更新用户登出时间
-    us.user[userid].offineTime = new Date();
-    //获取用户连接的文档消息
-    var _joinword = us.offLineMessage[pageid].word;
-    var _joinexcel = us.offLineMessage[pageid].excel;
-    var _docuser, _now; //记录用户在数组的位置
-    var i, j, o; //定义循环变量
-
-    //退出协同word
-    for (i = 0; i < _joinword.length; i++) {
-        _docuser = us.word[_joinword[i]].user[userid];
-        _now = _docuser.indexOf(pageid);
-        _now != -1 && us.word[_joinword[i]].user.splice(_now, 1);
-    }
-
-    //退出协同Excel
-    for (j = 0; j < _joinexcel.length; j++) {
-        _docuser = us.excel[_joinexcel[j]].user[userid];
-        _now = _docuser.indexOf(pageid);
-        _now != -1 && us.excel[_joinexcel[j]].user.splice(_now, 1);
-    }
+    var i, j, z; //定义循环变量
+    var _userid, _fileid, _message, _docinfo, _messageinfo,
+        _csclids = us.offLineMessage[pageid].cscl,
+        _wordids = us.offLineMessage[pageid].word, //得到加入的所有word
+        _excelids = us.offLineMessage[pageid].excel; //得到加入的所有excel
 
     //删除用户连接
     delete us.connect[userid][pageid];
@@ -557,8 +538,57 @@ us.offLine = function (response, userid, pageid) {
     //删除用户离校消息记录
     delete us.offLineMessage[pageid];
 
-    //相应退出
-    response.end('offLine');
+    //取消用户
+    if (us.connect[userid]) {
+        for (z in us.connect[userid]) { break }
+        if (!z) {
+            //用户登录数据清理,用于已经下线
+            delete us.user[userid];
+            delete us.connect[userid];
+        }
+    }
+
+    //知识架构的下线的处理
+    for (i = 0; i < _csclids.length; i++) { //循环处理每一个文档
+        _docinfo = us.cscl[_csclids[i]]; //记录文档信息
+        if (_docinfo) { //如果存在文档信息
+            _fileid = _csclids[i];
+            _userid = us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
+        }
+    }
+    //下线后通知所有的用户
+    if (_userid) {
+        _message = {};
+        var _messageinfo = us.message(userid, 'us.cscl', {
+            id: us.createGuid(),
+            type: "delUser",
+            userid: _userid,
+            docId: _fileid
+        });
+        _message[_fileid] = [_messageinfo];
+        us.broadcast(_message, pageid, "cscl");
+    }
+
+    //word的下线的处理
+    for (i = 0; i < _wordids.length; i++) { //循环处理每一个文档
+        _docinfo = us.word[_wordids[i]]; //记录文档信息
+        if (_docinfo) { //如果存在文档信息
+            us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
+            us.insertHistory(_docinfo, 'word'); //插入历史
+        }
+    }
+
+    //excel的下线的处理
+    for (j = 0; i < _excelids.length; j++) {
+        _docinfo = us.excel[_excelids[j]]; //记录文档信息
+        if (_docinfo) { //如果存在文档信息
+            us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
+            us.insertHistory(_docinfo, 'excel'); //插入历史
+        }
+    }
+
+
+
 }
 
 /**
@@ -568,7 +598,12 @@ us.offLine = function (response, userid, pageid) {
 * @param {string} userid 用户ID
 * @param {string} pageid 页面ID(用户登录唯一识别ID)
 **/
-us.polling = function (response, userid, pageid, type) {
+us.polling = function (requset, response, userid, pageid, type) {
+    // 监听请求的close事件
+    requset.on('close', () => {
+        console.log("--------" + userid + "-------------" + username);
+        us.offLine(response, userid, pageid)
+    });
     //判断轮询的用户是否在连接池中
     if (us.connect[userid]) {
         var _userarray = us.connect[userid], //用户连接池,获取制定用户连接的数量,
@@ -1475,8 +1510,6 @@ us.broadcast = function (messageinfo, pageid, type) {
         _recievedpageid, //接收用户的页面唯一识别ID
         _issend, // 是否发送
         _userconnect = us.connect; //用户连接池
-    // console.log("=================a=============");
-    // console.log(messageinfo);
     for (i in messageinfo) {
         _messageinfo = messageinfo[i]; //记录消息消息
         _users = us[type][i] ? us[type][i].user : {}; //记录用户集合
@@ -1586,6 +1619,7 @@ us.quitUserList = function (docinfo, userid, pageid) {
             //如果该用户的全部端用户已退出则删除用户的记录
             if (_users.length == 0) {
                 delete docinfo.user[userid];
+                return userid;
             }
         }
     }
@@ -1654,9 +1688,7 @@ us.logout = function (response, userid, pageid) {
         _docinfo = us.excel[_excelids[j]]; //记录文档信息
         if (_docinfo) { //如果存在文档信息
             us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
-
             delete us.offLineMessage[pageid]; //删除用户离线消息记录
-
             us.insertHistory(_docinfo, 'excel'); //插入历史
         }
     }