Skip to content

Commit bd41750

Browse files
committed
Finished heartbeat implementation. See issue danpaquin#28 for example.
1 parent d41b393 commit bd41750

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

GDAX/WebsocketClient.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from threading import Thread
1111
from websocket import create_connection
1212

13-
1413
class WebsocketClient(object):
1514
def __init__(self, url=None, products=None, type=None):
1615
if url is None:
@@ -43,30 +42,29 @@ def _connect(self):
4342
self.url = self.url[:-1]
4443

4544
self.stop = False
46-
sub_params = {'type': self.type}
47-
if self.type is "subscribe":
48-
sub_params['product_ids'] = self.products
49-
elif self.type is "heartbeat":
50-
sub_params['on'] = "true"
51-
print(json.dumps(sub_params))
45+
sub_params = {'type': 'subscribe', 'product_ids': self.products}
5246
self.ws.send(json.dumps(sub_params))
47+
if self.type is "heartbeat":
48+
sub_params = {"type": "heartbeat", "on": True}
49+
self.ws.send(json.dumps(sub_params))
5350

5451
def _listen(self):
5552
while not self.stop:
5653
try:
5754
msg = json.loads(self.ws.recv())
5855
except Exception as e:
5956
self.onError(e)
57+
self.close()
6058
else:
6159
self.onMessage(msg)
6260

6361
def close(self):
64-
if self.type is "heartbeat":
65-
self.ws.send({"type": "heartbeat", "on": "false"})
6662
if self.stop is False:
67-
self.ws.close()
63+
if self.type is "heartbeat":
64+
self.ws.send(json.dumps({"type": "heartbeat", "on": False}))
65+
self.stop = True
6866
self.onClose()
69-
self.stop = True
67+
self.ws.close()
7068

7169
def onOpen(self):
7270
print("-- Subscribed! --\n")

0 commit comments

Comments
 (0)