Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sammchardy committed Nov 24, 2017
2 parents bf66888 + 48f8ac6 commit 92d823a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
28 changes: 27 additions & 1 deletion binance/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
WebSocketClientProtocol, \
connectWS
from twisted.internet import reactor, ssl
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet.error import ReactorAlreadyRunning

from .enums import KLINE_INTERVAL_1MINUTE, WEBSOCKET_UPDATE_1SECOND, WEBSOCKET_DEPTH_1
Expand All @@ -17,12 +18,37 @@

class BinanceClientProtocol(WebSocketClientProtocol):

def onConnect(self, response):
# reset the delay after reconnecting
self.factory.resetDelay()

def onMessage(self, payload, isBinary):
if not isBinary:
payload_obj = json.loads(payload.decode('utf8'))
self.factory.callback(payload_obj)


class BinanceReconnectingClientFactory(ReconnectingClientFactory):

# set initial delay to a short time
initialDelay = 0.1

maxDelay = 10

maxRetries = 5


class BinanceClientFactory(WebSocketClientFactory, BinanceReconnectingClientFactory):

protocol = BinanceClientProtocol

def clientConnectionFailed(self, connector, reason):
self.retry(connector)

def clientConnectionLost(self, connector, reason):
self.retry(connector)


class BinanceSocketManager(threading.Thread):

_user_timeout = 50 * 60 # 50 minutes
Expand All @@ -47,7 +73,7 @@ def _start_socket(self, path, callback, update_time=WEBSOCKET_UPDATE_1SECOND):
if path in self._conns:
return False

factory = WebSocketClientFactory(BINANCE_STREAM_URL + path)
factory = BinanceClientFactory(BINANCE_STREAM_URL + path)
factory.protocol = BinanceClientProtocol
factory.callback = callback
context_factory = ssl.ClientContextFactory()
Expand Down
2 changes: 2 additions & 0 deletions docs/websockets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ When creating socket connections a callback function is passed which receives th

Messages are received as dictionary objects relating to the message formats defined in the `Binance API documentation <https://www.binance.com/restapipub.html#wss-endpoint>`_.

Websockets are setup to reconnect with a maximum of 5 retries.

Websocket Update Time
---------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/withdraw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Withdraw Endpoints

Make sure you enable Withdrawal permissions for your API Key to use this call.

You must have withdrawn to the address through the website and approved the withdraw via email before you can withdraw using the API.
You must have withdrawn to the address through the website and approved the withdrawal via email before you can withdraw using the API.

Raises a `BinanceWithdrawException <binance.html#binance.exceptions.BinanceWithdrawException>`_ if the withdraw fails.

Expand Down

0 comments on commit 92d823a

Please sign in to comment.