Forráskód Böngészése

[add][Refs #5][Fix #32] Send Active every minutes and close socket after 2 minutes of inactivity

B Thibault 8 éve
szülő
commit
8fa37320fc
3 módosított fájl, 32 hozzáadás és 15 törlés
  1. 1 1
      srv/src/httpServ.js
  2. 29 13
      srv/src/slack.js
  3. 2 1
      srv/src/slackManager.js

+ 1 - 1
srv/src/httpServ.js

@@ -120,7 +120,7 @@ Server.prototype.onRequest = function(req, res) {
         // Api / dynamic content
         var apiSuccess = false;
 
-        res.slack = slackManager.lazyGet(req.session);
+        res.slack = slackManager.lazyGet(req.session, req.reqT);
         if (req.urlObj.match(["api", "hist"])) {
             if (!req.urlObj.queryTokens.room) {
                 res.writeHeader("400", "Bad request");

+ 29 - 13
srv/src/slack.js

@@ -23,11 +23,13 @@ const SLACK_ENDPOINT = "https://slack.com/api/"
         ,editMsg: "chat.update"
         ,removeMsg: "chat.delete"
         ,postFile: "files.upload"
+        ,setActive: "users.setActive"
         ,emojiList: "emoji.list"
         ,addReaction: "reactions.add"
         ,removeReaction: "reactions.remove"
     }
     ,HISTORY_LENGTH = 35
+    ,INACTIVE_DELAY = 120000
 
     ,UPDATE_LIVE = [
         "message"
@@ -45,9 +47,10 @@ var
 ;
 
 setInterval(function() {
+    var t = Date.now();
     SLACK_SESSIONS.forEach(function(slackInst) {
-        if (!slackInst.closeIfInnactive())
-            slackInst.ping();
+        if (!slackInst.closeIfInnactive(t) && slackInst.active)
+            slackInst.sendActive();
     });
 }, 60000);
 
@@ -58,6 +61,11 @@ function Slack(sess) {
     this.history = {};
     this.connected = false;
     this.active = true;
+    this.lastPing = Date.now();
+}
+
+Slack.prototype.onUserInterract = function(t) {
+    this.lastPing = Math.max(t, this.lastPing);
 }
 
 Slack.prototype.onRequest = function(knownVersion, cb) {
@@ -221,32 +229,40 @@ Slack.prototype.connectRtm = function(url, cb) {
         _this.connected = false;
         console.error(e);
         _this.close();
+        var arrIndex = SLACK_SESSIONS.indexOf(_this);
+        if (arrIndex >= 0) SLACK_SESSIONS.splice(arrIndex, 1);
     });
     this.rtm.once("end", function() {
-        _this.connected = false;
         console.error("RTM hang up");
-        _this.close();
+        _this.onClose();
     });
 };
 
+Slack.prototype.onClose = function() {
+    var arrIndex = SLACK_SESSIONS.indexOf(this);
+    this.connected = false;
+    if (arrIndex >= 0) SLACK_SESSIONS.splice(arrIndex, 1);
+};
+
 /**
  * @return {boolean} true if innactive and closeding
 **/
-Slack.prototype.closeIfInnactive = function() {
-    //TODO
+Slack.prototype.closeIfInnactive = function(t) {
+    if (t - this.lastPing >INACTIVE_DELAY) {
+        this.close();
+        return true;
+    }
     return false;
 };
 
-Slack.prototype.ping = function() {
-    console.log("pinging");
-    this.rtm.send(JSON.stringify({
-        "id": ++this.rtmId
-        ,"type": "ping"
-        ,"set_active": this.active
-    }));
+Slack.prototype.sendActive = function() {
+    httpsRequest(SLACK_ENDPOINT+GETAPI.setActive
+        +"?token=" +this.token);
 };
 
 Slack.prototype.close = function() {
+    this.rtm.close();
+    this.onClose();
 };
 
 Slack.getOauthToken = function(code, redirectUri, cb) {

+ 2 - 1
srv/src/slackManager.js

@@ -13,7 +13,7 @@ SlackManager.prototype.getScope = function() {
     ];
 };
 
-SlackManager.prototype.lazyGet = function(sess) {
+SlackManager.prototype.lazyGet = function(sess, reqT) {
     var key = "SLACK_" +sess.sessId
         ,val = cache.get(key);
     if (val) {
@@ -26,6 +26,7 @@ SlackManager.prototype.lazyGet = function(sess) {
             instance.removed(key, val);
         });
     }
+    val.onUserInterract(reqT);
     return val;
 };