Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions cbpro/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def _go():

self.stop = False
self.on_open()
self.thread = Thread(target=_go)
self.keepalive = Thread(target=self._keepalive)
self.thread = Thread(target=_go, name='socket-worker-thread')
self.keepalive = Thread(target=self._keepalive, name='keepalive-thread')
self.thread.start()

def _connect(self):
Expand All @@ -70,12 +70,14 @@ def _connect(self):
sub_params['passphrase'] = auth_headers['CB-ACCESS-PASSPHRASE']
sub_params['timestamp'] = auth_headers['CB-ACCESS-TIMESTAMP']

self.ws = create_connection(self.url)

self.ws.send(json.dumps(sub_params))
try:
self.ws = create_connection(self.url)
self.ws.send(json.dumps(sub_params))
except Exception as e:
self.on_error(e)

def _keepalive(self, interval=30):
while self.ws.connected:
while self.ws and self.ws.connected:
self.ws.ping("keepalive")
time.sleep(interval)

Expand All @@ -99,7 +101,8 @@ def _disconnect(self):
except WebSocketConnectionClosedException as e:
pass
finally:
self.keepalive.join()
if self.keepalive.is_alive():
self.keepalive.join()

self.on_close()

Expand Down