From dc340d19b6f52cca1cb3cd43e5a8e772de65174a Mon Sep 17 00:00:00 2001 From: nanoric Date: Mon, 15 Oct 2018 04:56:51 -0400 Subject: [PATCH] =?UTF-8?q?[Add]=20WebSocketClient:=E5=A2=9E=E5=8A=A0setCr?= =?UTF-8?q?eateConnection=20[Add]=20WebSocketClient:=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/websocket/WebSocketClient.py | 29 +++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/vnpy/api/websocket/WebSocketClient.py b/vnpy/api/websocket/WebSocketClient.py index e208ce8f..1ae22dae 100644 --- a/vnpy/api/websocket/WebSocketClient.py +++ b/vnpy/api/websocket/WebSocketClient.py @@ -13,7 +13,23 @@ import websocket class WebSocketClient(object): - """Websocket API""" + """ + Websocket API + + 继承使用该类。 + 实例化之后,应调用start开始后台线程。调用start()函数会自动连接websocket。 + 若要终止后台线程,请调用stop()。 stop()函数会顺便断开websocket。 + + 可以重写以下函数: + onConnected + onDisconnected + onPacket + onError + + 当然,为了不让用户随意自定义,用自己的init函数覆盖掉原本的init(host)也是个不错的选择。 + + @note 继承使用该类 + """ #---------------------------------------------------------------------- def __init__(self): @@ -26,6 +42,15 @@ class WebSocketClient(object): self._workerThread = None # type: Thread self._pingThread = None # type: Thread self._active = False + + self.createConnection = websocket.create_connection + + #---------------------------------------------------------------------- + def setCreateConnection(self, func): + """ + for internal usage + """ + self.createConnection = func #---------------------------------------------------------------------- def init(self, host): @@ -78,7 +103,7 @@ class WebSocketClient(object): #---------------------------------------------------------------------- def _connect(self): """""" - self._ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) + self._ws = self.createConnection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) self.onConnected() #----------------------------------------------------------------------