def _websocket_on_message(self, contextId, data): msg = json.loads(data) # console.log(msg) lastSeqId = self._contextGet(contextId, 'sequence_id') seqId = self.safe_integer(msg, 'socket_sequence') if lastSeqId is not None: lastSeqId = lastSeqId + 1 if lastSeqId != seqId: self.emit('err', NetworkError('sequence id error in exchange: ' + self.id + '(' + str(lastSeqId) + '+1 !=' + str(seqId) + ')'), contextId) return self._contextSet(contextId, 'sequence_id', seqId) symbol = self._contextGet(contextId, 'symbol') msgType = msg['type'] if msgType == 'heartbeat': return if msgType == 'update': events = self.safe_value(msg, 'events', []) symbolData = None obEventActive = False subscribedEvents = self._contextGetEvents(contextId) if 'ob' in subscribedEvents: symbolData = self._contextGetSymbolData(contextId, 'ob', symbol) obEventActive = True eventsLength = len(events) if eventsLength > 0: event = events[0] if (event['type'] == 'change') and(self.safe_string(event, 'reason') == 'initial'): symbolData['ob'] = { 'bids': [], 'asks': [], 'timestamp': None, 'datetime': None, } elif event['type'] == 'change': timestamp = self.safe_float(msg, 'timestamp') timestamp = timestamp * 1000 symbolData['ob']['timestamp'] = timestamp symbolData['ob']['datetime'] = self.iso8601(timestamp) symbolData['ob']['nonce'] = self.safe_integer(msg, 'eventId') for i in range(0, len(events)): event = events[i] eventType = event['type'] if (eventType == 'change') and obEventActive: side = self.safe_string(event, 'side') price = self.safe_float(event, 'price') size = self.safe_float(event, 'remaining') keySide = 'bids' if (side == 'bid') else 'asks' self.updateBidAsk([price, size], symbolData['ob'][keySide], side == 'bid') elif eventType == 'trade' and('trade' in list(subscribedEvents.keys())): self._websocket_handle_trade(msg, event, symbol) if obEventActive: self.emit('ob', symbol, self._cloneOrderBook(symbolData['ob'], symbolData['limit'])) # True even with 'trade', as a trade event has the corresponding ob change event in the same events list self._contextSetSymbolData(contextId, 'ob', symbol, symbolData)
def _websocket_handle_ob_update(self, contextId, msg, oid, resData): symbol = resData['pair'].replace(':', '/') timestamp = resData['time'] data = self._contextGetSymbolData(contextId, 'ob', symbol) if data['ob']['nonce'] != (resData['id'] - 1): self.websocketClose() self.emit( 'err', NetworkError('invalid orderbook sequence in ' + self.id + ' ' + data['ob']['nonce'] + ' != ' + resData['id'] + ' -1')) else: ob = self.mergeOrderBookDelta(data['ob'], resData, timestamp) ob['nonce'] = resData['id'] data['ob'] = ob self._contextSetSymbolData(contextId, 'ob', symbol, data) self.emit('ob', symbol, self._cloneOrderBook(data['ob'], data['limit']))
def _websocket_check_sequence(self, contextId, msg): msgData = self.safe_value(msg, 'data') if msgData is None: return sequeceId = self.safe_integer(msgData, 'sequence') if sequeceId is None: return chan = self.safe_string(msg, 'channel') lastSeqIdData = self._contextGet(contextId, 'sequence') if lastSeqIdData is None: lastSeqIdData = {} if chan in lastSeqIdData: lastSeqId = lastSeqIdData[chan] lastSeqId = self.sum(lastSeqId, 1) if sequeceId != lastSeqId: self.emit('err', NetworkError('sequence error in pusher connection ' + sequeceId + ' != ' + lastSeqId), contextId) return lastSeqIdData[chan] = sequeceId self._contextSet(contextId, 'sequence', lastSeqIdData)
def websocket_connection_error(error): websocket_connection_info['auth'] = False self._websocket_on_error(conxid) # self._websocket_reset_context(conxid) self.emit('err', NetworkError(error), conxid)