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 book_depth_channel(symbol, limit): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@depth" + str(limit)) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def diff_depth_channel(symbol): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@depth") channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def kline_channel(symbol, interval): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@kline_" + interval) channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def all_bookticker_channel(): channel = dict() channel["params"] = list() channel["params"].append("!bookTicker") channel["id"] = get_current_timestamp() channel["method"] = "SUBSCRIBE" return json.dumps(channel)
def symbol_miniticker_channel(symbol): channel = dict() channel["params"] = list() channel["params"].append(symbol + "@miniTicker") 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 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
def on_message(self, message): self.last_receive_time = get_current_timestamp() json_wrapper = parse_json_from_string(message) if 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)