1- #
2- # GDAX/WebsocketClient.py
3- # Daniel Paquin
4- #
5- # Template object to receive messages from the GDAX Websocket Feed
1+ """
2+ GDAX/WebsocketClient.py
3+ Daniel Paquin
4+
5+ Template object to receive messages from the GDAX Websocket Feed
6+ """
7+
8+ from __future__ import print_function
9+ import json
10+ import time
11+ from threading import Thread
12+ from websocket import create_connection
613
7- import json , time
8- from threading import Thread
9- from websocket import create_connection
1014
1115class WebsocketClient (object ):
12- def __init__ (self , url = "wss://ws-feed.gdax.com" , products = [ "BTC-USD" ]):
16+ def __init__ (self , url = "wss://ws-feed.gdax.com" , products = ["BTC-USD" ]):
1317 self .url = url
1418 if self .url [- 1 ] == "/" :
1519 self .url = self .url [:- 1 ]
@@ -25,7 +29,6 @@ def _go():
2529 self .thread = Thread (target = _go )
2630 self .thread .start ()
2731
28-
2932 def _connect (self ):
3033 self .stop = False
3134 subParams = json .dumps ({"type" : "subscribe" , "product_ids" : self .products })
@@ -58,22 +61,28 @@ def onMessage(self, msg):
5861 def onError (self , e ):
5962 SystemError (e )
6063
64+
6165if __name__ == "__main__" :
6266 import GDAX
67+
68+
6369 class myWebsocketClient (GDAX .WebsocketClient ):
6470 def onOpen (self ):
6571 self .MessageCount = 0
66- print "Lets count the messages!"
72+ print ("Lets count the messages!" )
73+
6774 def onMessage (self , msg ):
68- print "Message type:" , msg ["type" ], "\t @ %.3f" % float (msg ["price" ])
75+ print ( "Message type:" , msg ["type" ], "\t @ %.3f" % float (msg ["price" ]) )
6976 self .MessageCount += 1
77+
7078 def onClose (self ):
71- print "-- Goodbye! --"
79+ print ("-- Goodbye! --" )
80+
7281
7382 wsClient = myWebsocketClient ()
7483 wsClient .start ()
7584 # Do some logic with the data
7685 while (wsClient .MessageCount < 500 ):
77- print "\n MessageCount =" , "%i \n " % wsClient .MessageCount
86+ print ( "\n MessageCount =" , "%i \n " % wsClient .MessageCount )
7887 time .sleep (1 )
79- wsClient .close ()
88+ wsClient .close ()
0 commit comments