- provide loop at ws::connect
This commit is contained in:
+9
-3
@@ -5,7 +5,7 @@ from ws.client.ws_client import WsClient
|
||||
from ws.message import MessageDispatcher
|
||||
|
||||
|
||||
class Window(QtWidgets.QMainWindow):
|
||||
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
def __init__(self):
|
||||
QtWidgets.QMainWindow.__init__(self)
|
||||
self.msg_dispatch = MessageDispatcher()
|
||||
@@ -18,8 +18,16 @@ class Window(QtWidgets.QMainWindow):
|
||||
self.msg_sensor.set_recv_handler(self.on_sensor_changed)
|
||||
self.msg_heater.set_recv_handler(self.on_heater_changed)
|
||||
|
||||
self.setupUi(self)
|
||||
self.actionStart.triggered.connect(self.connect)
|
||||
self.actionStop.triggered.connect(self.disconnect)
|
||||
|
||||
def connect(self):
|
||||
self.ws_client.connect(uri="ws://localhost:8765")
|
||||
|
||||
def disconnect(self):
|
||||
self.ws_client.disconnect()
|
||||
|
||||
def on_pot_changed(self, msg):
|
||||
print("on_pot_changed")
|
||||
|
||||
@@ -51,8 +59,6 @@ class Window(QtWidgets.QMainWindow):
|
||||
if __name__ == '__main__':
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
win = Window()
|
||||
ui = Ui_MainWindow()
|
||||
ui.setupUi(win)
|
||||
win.show()
|
||||
sys.exit(app.exec_())
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ class WsClient:
|
||||
return
|
||||
|
||||
print("handler: got connection to {}".format(websocket.remote_address))
|
||||
self.listener.on_connect()
|
||||
self.listener.on_connect(self.loop)
|
||||
try:
|
||||
path = "/"
|
||||
consumer_task = asyncio.ensure_future(self.handler_recv(websocket, path), loop=self.loop)
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ import abc
|
||||
|
||||
class IConnection:
|
||||
@abc.abstractmethod
|
||||
def on_connect(self):
|
||||
def on_connect(self, loop):
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
|
||||
+3
-3
@@ -47,7 +47,7 @@ class Value:
|
||||
class MessageDispatcher(IConnection):
|
||||
def __init__(self):
|
||||
self.msg_handlers: MsgIo = []
|
||||
self.state = asyncio.Queue()
|
||||
self.state = None
|
||||
|
||||
def msgio_get(self, key):
|
||||
obj = MsgIo(key, self.send)
|
||||
@@ -56,8 +56,8 @@ class MessageDispatcher(IConnection):
|
||||
return obj
|
||||
return None
|
||||
|
||||
def on_connect(self):
|
||||
pass
|
||||
def on_connect(self, loop):
|
||||
self.state = asyncio.Queue(loop=loop)
|
||||
|
||||
def on_disconnect(self):
|
||||
pass
|
||||
|
||||
@@ -21,6 +21,7 @@ class WsServerMultiUser(WsServer):
|
||||
self.listener = listener
|
||||
self.USERS = UserSet()
|
||||
self.global_state = {}
|
||||
self.listener.on_connect(self.loop)
|
||||
|
||||
async def notify_state(self, data):
|
||||
if self.USERS: # asyncio.wait doesn't accept an empty list
|
||||
|
||||
Reference in New Issue
Block a user