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