11# coding=utf-8
2+ import gzip
23import threading
34
45from autobahn .twisted .websocket import WebSocketClientFactory , \
@@ -22,13 +23,18 @@ def onConnect(self, response):
2223 self .factory .resetDelay ()
2324
2425 def onMessage (self , payload , isBinary ):
25- if not isBinary :
26+ if isBinary :
2627 try :
27- payload_obj = json .loads (payload .decode ('utf8' ))
28- except ValueError :
29- pass
30- else :
31- self .factory .callback (payload_obj )
28+ payload = gzip .decompress (payload )
29+ except :
30+ print ('Could not interpret binary response payload' )
31+
32+ try :
33+ payload_obj = json .loads (payload .decode ('utf8' ))
34+ except ValueError :
35+ pass
36+ else :
37+ self .factory .callback (payload_obj )
3238
3339
3440class BinanceReconnectingClientFactory (ReconnectingClientFactory ):
@@ -64,6 +70,7 @@ class BinanceSocketManager(threading.Thread):
6470
6571 STREAM_URL = 'wss://stream.binance.com:9443/'
6672 FSTREAM_URL = 'wss://fstream.binance.com/'
73+ VSTREAM_URL = 'wss://vstream.binance.com/'
6774
6875 WEBSOCKET_DEPTH_5 = '5'
6976 WEBSOCKET_DEPTH_10 = '10'
@@ -120,6 +127,20 @@ def _start_futures_socket(self, path, callback, prefix='stream?streams='):
120127 self ._conns [path ] = connectWS (factory , context_factory )
121128 return path
122129
130+ def _start_options_socket (self , path , callback , prefix = 'ws/' ):
131+ if path in self ._conns :
132+ return False
133+
134+ factory_url = self .VSTREAM_URL + prefix + path
135+ factory = BinanceClientFactory (factory_url )
136+ factory .protocol = BinanceClientProtocol
137+ factory .callback = callback
138+ factory .reconnect = True
139+ context_factory = ssl .ClientContextFactory ()
140+
141+ self ._conns [path ] = connectWS (factory , context_factory )
142+ return path
143+
123144 def start_depth_socket (self , symbol , callback , depth = None , interval = None ):
124145 """Start a websocket for symbol market depth returning either a diff or a partial book
125146
@@ -698,6 +719,58 @@ def start_isolated_margin_socket(self, symbol, callback):
698719 # and start the socket with this specific kek
699720 return self ._start_account_socket (symbol , isolated_margin_listen_key , callback )
700721
722+ def start_options_ticker_socket (self , symbol , callback ):
723+ """Subscribe to a 24 hour ticker info stream
724+
725+ https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-24-hour-ticker
726+
727+ :param symbol: required
728+ :type symbol: str
729+ :param callback: callback function to handle messages
730+ :type callback: function
731+ """
732+ return self ._start_options_socket (symbol .lower () + '@ticker' , callback )
733+
734+ def start_options_recent_trades_socket (self , symbol , callback ):
735+ """Subscribe to a latest completed trades stream
736+
737+ https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-latest-completed-trades
738+
739+ :param symbol: required
740+ :type symbol: str
741+ :param callback: callback function to handle messages
742+ :type callback: function
743+ """
744+ return self ._start_options_socket (symbol .lower () + '@trade' , callback )
745+
746+ def start_options_kline_socket (self , symbol , callback , interval = Client .KLINE_INTERVAL_1MINUTE ):
747+ """Subscribe to a candlestick data stream
748+
749+ https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-candle
750+
751+ :param symbol: required
752+ :type symbol: str
753+ :param callback: callback function to handle messages
754+ :type callback: function
755+ :param interval: Kline interval, default KLINE_INTERVAL_1MINUTE
756+ :type interval: str
757+ """
758+ return self ._start_options_socket (symbol .lower () + '@kline_' + interval , callback )
759+
760+ def start_options_depth_socket (self , symbol , callback , depth = '10' ):
761+ """Subscribe to a depth data stream
762+
763+ https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-depth
764+
765+ :param symbol: required
766+ :type symbol: str
767+ :param callback: callback function to handle messages
768+ :type callback: function
769+ :param depth: optional Number of depth entries to return, default 10.
770+ :type depth: str
771+ """
772+ return self ._start_options_socket (symbol .lower () + '@depth' + str (depth ), callback )
773+
701774 def _start_account_socket (self , socket_type , listen_key , callback ):
702775 """Starts one of user or margin socket"""
703776 self ._check_account_socket_open (listen_key )
0 commit comments