File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010from threading import Thread
1111from websocket import create_connection
1212
13-
1413class 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 " )
You can’t perform that action at this time.
0 commit comments