Wait for websocket to connect before interacting with messagebus
@@ -17,6 +17,7 @@ import time
import monotonic
from multiprocessing.pool import ThreadPool
+from threading import Event
from pyee import EventEmitter
from websocket import WebSocketApp
@@ -43,6 +44,7 @@ class WebsocketClient(object):
self.client = self.create_client()
self.pool = ThreadPool(10)
self.retry = 5
+ self.connected_event = Event()
@staticmethod
def build_url(host, port, route, ssl):
@@ -59,6 +61,7 @@ class WebsocketClient(object):
self.emitter.emit("open")
# Restore reconnect timer to 5 seconds on sucessful connect
self.retry = 5
+ self.connected_event.set()
def on_close(self, ws):
self.emitter.emit("close")
@@ -82,6 +85,7 @@ class WebsocketClient(object):
self.emitter.emit, (parsed_message.type, parsed_message))
def emit(self, message):
+ self.connected_event.wait(10)
if (not self.client or not self.client.sock or
not self.client.sock.connected):
return
@@ -145,6 +149,7 @@ class WebsocketClient(object):
def close(self):
self.client.close()
+ self.connected_event.clear()
def echo():
