changeset 1351:6044e527d795

ping loop (and some lines from the next patch). My setup seemed to be dropping WS after like 30 sec. Ignore-this: 7d2150afcf21628882d78914898c04d4
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 05 Jun 2016 03:39:39 +0000
parents 36f58b2aa8ef
children d9a866bfb9d5
files bin/rdfdb light9/web/graph.coffee
diffstat 2 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/bin/rdfdb	Sun Jun 05 03:21:31 2016 +0000
+++ b/bin/rdfdb	Sun Jun 05 03:39:39 2016 +0000
@@ -417,6 +417,9 @@
             Failure(WebsocketDisconnect(reason)), self.wsClient)
 
     def messageReceived(self, message):
+        if message == 'PING':
+            self.sendMessage('PONG')
+            return
         log.info("got message from %r: %s", self.wsClient, message)
         p = Patch(jsonRepr=message)
         p.senderUpdateUri = self.wsClient.updateUri
--- a/light9/web/graph.coffee	Sun Jun 05 03:21:31 2016 +0000
+++ b/light9/web/graph.coffee	Sun Jun 05 03:39:39 2016 +0000
@@ -77,6 +77,8 @@
 
     @ws.onopen = =>
       log('connected to', fullUrl)
+      @resetStore()
+      @pingLoop()
 
     @ws.onerror = (e) =>
       log('ws error ' + e)
@@ -85,8 +87,17 @@
       log('ws close')
 
     @ws.onmessage = (evt) =>
+      if evt.data == 'PONG'
+        return
       @onMessage(JSON.parse(evt.data))
 
+  pingLoop: () ->
+    if @ws.readyState == @ws.OPEN
+      @ws.send('PING')
+      
+      clearTimeout(@_pingLoopTimeout) if @_pingLoopTimeout?
+      @_pingLoopTimeout = setTimeout(@pingLoop.bind(@), 10000)
+
   onMessage: (msg) ->
     log('from rdfdb: ', msg)