Quellcode durchsuchen

[add][Refs #5] Try to send ping messages every minutes

B Thibault vor 8 Jahren
Ursprung
Commit
d0d6ea2626
1 geänderte Dateien mit 18 neuen und 3 gelöschten Zeilen
  1. 18 3
      srv/src/slack.js

+ 18 - 3
srv/src/slack.js

@@ -46,7 +46,8 @@ var
 
 setInterval(function() {
     SLACK_SESSIONS.forEach(function(slackInst) {
-        slackInst.closeIfInnactive();
+        if (!slackInst.closeIfInnactive())
+            slackInst.ping();
     });
 }, 60000);
 
@@ -203,14 +204,17 @@ Slack.prototype.onMessage = function(msg) {
 Slack.prototype.connectRtm = function(url, cb) {
     var _this = this;
 
+    this.rtmId = 0;
     this.rtm = new WebSocket(url);
     this.rtm.on("message", function(msg) {
         if (!_this.connected && cb) {
             cb();
         }
-        _this.connected = true;
+        if (!_this.connected) {
+            _this.connected = true;
+            SLACK_SESSIONS.push(_this);
+        }
         _this.onMessage(JSON.parse(msg));
-        SLACK_SESSIONS.push(_this);
     });
     this.rtm.once("error", function(e) {
         _this.connected = false;
@@ -224,8 +228,19 @@ Slack.prototype.connectRtm = function(url, cb) {
     });
 };
 
+/**
+ * @return {boolean} true if innactive and closeding
+**/
 Slack.prototype.closeIfInnactive = function() {
     //TODO
+    return false;
+};
+
+Slack.prototype.ping = function() {
+    this.rtm.send(JSON.stringify({
+        "id": ++this.rtmId
+        ,"type": "ping"
+    }));
 };
 
 Slack.prototype.close = function() {