ws_client.py 946 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import websocket
  2. import threading
  3. from logger_config import logger
  4. from data_processing import on_message_depth, stop_event
  5. # Binance WebSocket API URL
  6. SYMBOL = "ethfi" + "usdt"
  7. SOCKET_DEPTH = "wss://fstream.binance.com/stream?streams=" + SYMBOL + "@depth20@100ms"
  8. def on_error(_ws, error):
  9. logger.error(error)
  10. def on_open(_ws):
  11. logger.info("### binance_ws opened ###")
  12. # Create a WebSocket app
  13. ws_depth = websocket.WebSocketApp(SOCKET_DEPTH, on_message=on_message_depth, on_error=on_error, on_open=on_open)
  14. # 定义要传递给 run_forever 的参数
  15. http_proxy_host = "127.0.0.1"
  16. http_proxy_port = 7890
  17. proxy_type = "http"
  18. depth_thread = threading.Thread(target=ws_depth.run_forever, kwargs={
  19. 'http_proxy_host': http_proxy_host,
  20. 'http_proxy_port': http_proxy_port,
  21. 'proxy_type': proxy_type
  22. })
  23. def start_ws_clients():
  24. depth_thread.start()
  25. def stop_all_threads():
  26. stop_event.set()
  27. depth_thread.join()