简体   繁体   English

如何重新连接 websocket 连接 websocket-client

[英]How to reconnect a websocket connection websocket-client

I've been trying to write code that collects crypto data from Binance.我一直在尝试编写从 Binance 收集加密数据的代码。 Binance auto disconnects after 24 hours.币安会在 24 小时后自动断开连接。 Is there any way for me to reconnect after disconnection?断线后有什么办法让我重新连接吗? I believe running forever should take care of that for me, but it dies when an error is thrown.我相信永远运行应该为我解决这个问题,但是当抛出错误时它会死掉。 I will be running this program on a server 24/7.我将 24/7 在服务器上运行这个程序。 I will also need a way to be notified maybe telegram/discord bot that I can build where do I type the code to send when it is disconnected我还需要一种方法来获得通知,可能是电报/不和谐机器人,我可以构建我在哪里键入代码以在断开连接时发送

This is the error I get.这是我得到的错误。

Traceback (most recent call last):
  File "exchanges/binance/binance_ticker.py", line 97, in <module>
    start()
  File "exchanges/binance/binance_ticker.py", line 94, in start
    rel.dispatch()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/rel.py", line 205, in dispatch
    registrar.dispatch()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 72, in dispatch
    if not self.loop():
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 81, in loop
    e = self.check_events()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 232, in check_events
    self.callback('read', fd)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/registrar.py", line 125, in callback
    self.events[etype][fd].callback()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/rel/listener.py", line 108, in callback
    if not self.cb(*self.args) and not self.persist and self.active:
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_app.py", line 349, in read
    op_code, frame = self.sock.recv_data_frame(True)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_core.py", line 401, in recv_data_frame
    frame = self.recv_frame()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_core.py", line 440, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_abnf.py", line 352, in recv_frame
    payload = self.recv_strict(length)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_abnf.py", line 373, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_core.py", line 524, in _recv
    return recv(self.sock, bufsize)
  File "/home/pyjobs/.local/lib/python3.8/site-packages/websocket/_socket.py", line 122, in recv
    raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.

My code:我的代码:

import websocket
import rel


uri = "wss://stream.binance.com:9443/ws/!ticker@arr"


def on_message(ws, message):
print(message)


def on_error(ws, error):
print(error)
write_logs(error)


def on_close(ws, close_status_code, close_msg):
print("### closed ###")
write_logs(str(close_status_code) + str(close_msg))
start(


def on_open(ws):
print("Opened connection")


start()
websocket.enableTrace(True)
ws = websocket.WebSocketApp(uri,
on_open = on_open,
on_message=on_message,
on_error = on_error,
on_close (on_close)
ws.run_forever(dispatcher=rel) #Set the dispatcher to automatic reconnection.
rel.signal(2, rel.abort) # Keyboard Interrupt
rel.dispatch()


    
start()

The comment in this line of code ws.run_forever(dispatcher=rel) #Set the dispatcher to automatic reconnection.这行代码中的注释ws.run_forever(dispatcher=rel) #Set the dispatcher to automatic reconnection. could auto reconnection depending on rel module?可以根据 rel 模块自动重新连接吗? And how the module rel and func dispatcher work together?模块 rel 和 func 调度程序如何协同工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM