Toggle Connect/Disconnect on the same button once connected
MessageDispatcherSync gains an optional on_connect_changed(bool) callback, fired from the websocket client's background loop once the connection is actually live/lost, so the button only flips state on a real connection change rather than optimistically on click.
This commit is contained in:
+8
-1
@@ -103,6 +103,10 @@ class MessageDispatcherSync(IConnection):
|
||||
self.state = None
|
||||
self.state = queue.Queue()
|
||||
self.auto_subscribe = auto_subscribe
|
||||
# Optional callback(connected: bool), invoked from the websocket
|
||||
# client's background thread/loop - e.g. for a GUI to toggle a
|
||||
# Connect/Disconnect button once the connection is actually live.
|
||||
self.on_connect_changed = None
|
||||
|
||||
def msgio_get(self, key):
|
||||
obj = MsgIoSync(key, self.send)
|
||||
@@ -114,6 +118,8 @@ class MessageDispatcherSync(IConnection):
|
||||
async def on_connect(self):
|
||||
if self.auto_subscribe:
|
||||
asyncio.get_event_loop().run_in_executor(None, self.subscribe)
|
||||
if self.on_connect_changed is not None:
|
||||
self.on_connect_changed(True)
|
||||
|
||||
def subscribe(self):
|
||||
for handler in self.msg_handlers:
|
||||
@@ -122,7 +128,8 @@ class MessageDispatcherSync(IConnection):
|
||||
self.send({'+': key})
|
||||
|
||||
async def on_disconnect(self):
|
||||
pass
|
||||
if self.on_connect_changed is not None:
|
||||
self.on_connect_changed(False)
|
||||
|
||||
async def on_recv(self, data):
|
||||
d = json.loads(data)
|
||||
|
||||
Reference in New Issue
Block a user