home · contact · privacy
Make curses client PING.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 9 Nov 2020 21:45:51 +0000 (22:45 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 9 Nov 2020 21:45:51 +0000 (22:45 +0100)
new2/plomrogue/io_websocket.py
new2/rogue_chat_curses.py

index ea1659711d5d5fbe45e074de6eeaf11d9a65c67b..0a74f4e8a4ef8c2397fa99497cce16dc10a1d929 100644 (file)
@@ -13,7 +13,7 @@ class PlomWebSocket(WebSocket):
                 if self.server.clients[connection_id] == self:
                     self.server.queue.put((connection_id, self.data))
                     break
-    
+
     def handleConnected(self):
         import uuid
         print('CONNECTION FROM:', self.address)
index ee2cae8f5abad5682ef5fa7e17a7cf15e8cfdf4a..15d0053a8bca770f385aa9e8a70a2f707f415c55 100755 (executable)
@@ -139,8 +139,13 @@ def cmd_ANNOTATION(game, position, msg):
         game.tui.do_refresh = True
 cmd_ANNOTATION.argtypes = 'yx_tuple:nonneg string'
 
+def cmd_PONG(game):
+    pass
+cmd_PONG.argtypes = ''
+
 class Game(GameBase):
     commands = {'LOGIN_OK': cmd_LOGIN_OK,
+                'PONG': cmd_PONG,
                 'CHAT': cmd_CHAT,
                 'PLAYER_ID': cmd_PLAYER_ID,
                 'TURN': cmd_TURN,
@@ -296,6 +301,7 @@ class TUI:
 
     def loop(self, stdscr):
         import time
+        import datetime
 
         def safe_addstr(y, x, line):
             if y < self.size.y - 1 or x + len(line) < self.size.x:
@@ -492,7 +498,13 @@ class TUI:
         self.input_ = ''
         input_prompt = '> '
         connect()
+        last_ping = datetime.datetime.now()
+        interval = datetime.timedelta(seconds=30)
         while True:
+            now = datetime.datetime.now()
+            if now - last_ping > interval:
+                self.send('PING')
+                last_ping = now
             if self.do_refresh:
                 draw_screen()
                 self.do_refresh = False