From 4d7c3bf417f13dbe16e0b3a9bc03bdd250811c76 Mon Sep 17 00:00:00 2001 From: nanoric Date: Mon, 15 Oct 2018 04:35:38 -0400 Subject: [PATCH] =?UTF-8?q?[Add]=20WebSocketClient:=E5=A2=9E=E5=8A=A0onDis?= =?UTF-8?q?connected?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/api/base/WebSocketClientTest.py | 2 +- vnpy/api/websocket/WebSocketClient.py | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/api/base/WebSocketClientTest.py b/tests/api/base/WebSocketClientTest.py index 9f5d3da8..1bf1b923 100644 --- a/tests/api/base/WebSocketClientTest.py +++ b/tests/api/base/WebSocketClientTest.py @@ -17,7 +17,7 @@ class TestWebsocketClient(WebsocketClient): self.p.set_result(packet) pass - def onConnect(self): + def onConnected(self): pass def onError(self, exceptionType, exceptionValue, tb): diff --git a/vnpy/api/websocket/WebSocketClient.py b/vnpy/api/websocket/WebSocketClient.py index f1dee894..750a593f 100644 --- a/vnpy/api/websocket/WebSocketClient.py +++ b/vnpy/api/websocket/WebSocketClient.py @@ -43,7 +43,7 @@ class WebsocketClient(object): self._pingThread = Thread(target=self._runPing) self._pingThread.start() - self.onConnect() + self.onConnected() #---------------------------------------------------------------------- def stop(self): @@ -79,7 +79,7 @@ class WebsocketClient(object): def _connect(self): """""" self._ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) - self.onConnect() + self.onConnected() #---------------------------------------------------------------------- def _disconnect(self): @@ -104,6 +104,7 @@ class WebsocketClient(object): try: stream = ws.recv() if not stream: + self.onDisconnected() if self._active: self._reconnect() continue @@ -128,9 +129,17 @@ class WebsocketClient(object): return self._get_ws().send('ping', websocket.ABNF.OPCODE_PING) #---------------------------------------------------------------------- - @abstractmethod - def onConnect(self): - """连接回调""" + def onConnected(self): + """ + 连接成功回调 + """ + pass + + #---------------------------------------------------------------------- + def onDisconnected(self): + """ + 连接断开回调 + """ pass #----------------------------------------------------------------------