forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsockets.py
More file actions
179 lines (123 loc) · 4.48 KB
/
Copy pathwebsockets.py
File metadata and controls
179 lines (123 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# coding=utf-8
import json
import threading
from autobahn.twisted.websocket import WebSocketClientFactory, \
WebSocketClientProtocol, \
connectWS
from twisted.internet import reactor, ssl
BINANCE_STREAM_URL = 'wss://stream.binance.com:9443/ws/'
class BinanceClientProtol(WebSocketClientProtocol):
def onMessage(self, payload, isBinary):
if not isBinary:
payload_obj = json.loads(payload.decode('utf8'))
self.factory.callback(payload_obj)
class BinanceSocketManager(threading.Thread):
_conns = {}
_user_timer = None
_client = None
_user_listen_key = None
_user_timeout = 50 * 60 # 50 minutes
def __init__(self, client):
"""Intialise the BinanceSocketManager
:param client: Binance API client
:type client: binance.Client
"""
threading.Thread.__init__(self)
self._client = client
def _start_socket(self, path, callback):
if path in self._conns:
return False
factory = WebSocketClientFactory(BINANCE_STREAM_URL + path)
factory.protocol = BinanceClientProtol
factory.callback = callback
context_factory = ssl.ClientContextFactory()
self._conns[path] = connectWS(factory, context_factory)
return True
def start_depth_socket(self, symbol, callback):
"""Start a websocket for symbol market depth
:param symbol:
:param callback:
:returns: bool True if successful
"""
return self._start_socket(symbol.lower() + '@depth', callback)
def start_kline_socket(self, symbol, callback):
"""Start a websocket for symbol kline data
:param symbol:
:param callback:
:returns: bool True if successful
"""
return self._start_socket(symbol.lower() + '@kline', callback)
def start_trade_socket(self, symbol, callback):
"""Start a websocket for symbol trade data
:param symbol:
:param callback:
:returns: bool True if successful
"""
return self._start_socket(symbol.lower() + '@trade', callback)
def start_ticker_socket(self, callback):
"""Start a websocket for ticker data
:param callback:
:returns: bool True if successful
"""
return self._start_socket('!ticker@arr', callback)
def start_user_socket(self, callback):
"""Start a websocket for user data
:param callback:
:returns: bool True if successful
"""
self._user_listen_key = self._client.stream_get_listen_key()
if self._start_socket(self._user_listen_key, callback):
# start timer to keep socket alive
self._start_user_timer()
def _start_user_timer(self):
self._user_timer = threading.Timer(self._user_timeout, self._keepalive_user_socket)
self._user_timer.setDaemon(True)
self._user_timer.start()
def _keepalive_user_socket(self):
self._client.stream_keepalive(listenKey=self._user_listen_key)
self._start_user_timer()
def _stop_socket(self, path):
if path not in self._conns:
return
self._conns[path].disconnect()
del(self._conns[path])
def stop_depth_socket(self, symbol):
"""Stop the symbol depth socket
:param symbol:
"""
self._stop_socket(symbol.lower() + '@depth')
def stop_kline_socket(self, symbol):
"""Stop the symbol kline socket
:param symbol:
"""
self._stop_socket(symbol.lower() + '@kline')
def stop_trade_socket(self, symbol):
"""Stop the symbol trade socket
:param symbol:
"""
self._stop_socket(symbol.lower() + '@trade')
def stop_ticker_socket(self):
"""Stop the ticker data websocket
"""
self._stop_socket('!ticker@arr')
def stop_user_socket(self):
"""Stop the user data websocket
"""
if not self._user_listen_key:
return
self._stop_socket(self._user_listen_key)
# stop the timer
self._user_timer.cancel()
self._user_timer = None
# close the stream
self._client.stream_close(listenKey=self._user_listen_key)
self._user_listen_key = None
def run(self):
reactor.run(installSignalHandlers=False)
def close(self):
"""Stop the websocket manager and close connections
"""
reactor.stop()
self.stop_user_socket()
self._conns = {}