def on_message(self, message): self.last_receive_time = get_current_timestamp() self.logger.debug('Type of message is %s' % type(message)) if not isinstance(message, str): message = gzip.decompress(message).decode('utf-8') self.logger.debug(message) json_wrapper = parse_json_from_string(message) if json_wrapper.contain_key("method") and json_wrapper.get_string( "method") == "PING": self.__process_ping_on_new_spec(json_wrapper.get_int("E")) elif json_wrapper.contain_key( "status") and json_wrapper.get_string("status") != "ok": error_code = json_wrapper.get_string_or_default( "err-code", "Unknown error") error_msg = json_wrapper.get_string_or_default( "err-msg", "Unknown error") self.on_error(error_code + ": " + error_msg) elif json_wrapper.contain_key( "err-code") and json_wrapper.get_int("err-code") != 0: error_code = json_wrapper.get_string_or_default( "err-code", "Unknown error") error_msg = json_wrapper.get_string_or_default( "err-msg", "Unknown error") self.on_error(error_code + ": " + error_msg) elif json_wrapper.contain_key("result") and json_wrapper.contain_key( "id"): self.__on_receive_response(json_wrapper) else: self.__on_receive_payload(json_wrapper)
def symbol_ticker_channel(symbol): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@ticker") channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def index_kline_channel(pair, interval): channel = dict() channel["params"] = list() channel["params"].append(pair + "@indexPriceKline_" + interval) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def all_miniticker_channel(): channel = dict() channel["params"] = list() channel["params"].append("!miniTicker@arr") channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def aggregate_trade_channel(symbol): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@aggTrade") channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def mark_price_kline_channel(symbol, interval): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@markPriceKline_" + interval) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def user_data_channel(listenKey): channel = dict() channel["params"] = list() channel["params"].append(listenKey) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def diff_depth_channel(symbol, update_time): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@depth" + update_time) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def book_depth_channel(symbol, limit, update_time): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@depth" + str(limit) + str(update_time)) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def all_liquidation_channel(symbol): channel = dict() channel["params"] = list() channel["params"].append("!forceOrder@arr") channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def index_price_channel(pair): channel = dict() channel["params"] = list() channel["params"].append(pair + "@indexPrice") channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def continuous_kline_channel(pair, contract_type, interval): channel = dict() channel["params"] = list() channel["params"].append(pair + '_' + contract_type + "@continuousKline_" + interval) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def on_open(self, ws): self.logger.info("[Sub][" + str(self.id) + "] Connected to server") self.ws = ws self.last_receive_time = get_current_timestamp() self.state = ConnectionState.CONNECTED self.__watch_dog.on_connection_created(self) if self.request.subscription_handler is not None: self.request.subscription_handler(self) return
def watch_dog_job(*args): watch_dog_instance = args[0] for connection in watch_dog_instance.connection_list: if connection.state == ConnectionState.CONNECTED: if watch_dog_instance.is_auto_connect: ts = get_current_timestamp() - connection.last_receive_time if ts > watch_dog_instance.receive_limit_ms: watch_dog_instance.logger.warning( "[Sub][" + str(connection.id) + "] No response from server") connection.re_connect_in_delay( watch_dog_instance.connection_delay_failure) elif connection.in_delay_connection(): watch_dog_instance.logger.warning("[Sub] call re_connect") connection.re_connect() pass elif connection.state == ConnectionState.CLOSED_ON_ERROR: if watch_dog_instance.is_auto_connect: connection.re_connect_in_delay( watch_dog_instance.connection_delay_failure) pass