'How to handle connection reset in Python SignalR and reconnect again

I am using Python3, signalr, and WebSocket to connect to a server.

When the connection is closed or reset by the server or the network layer, I receive the following error in the output but my print_error function is not fired. How can I handle this error and reconnect the connection?

Below is the connection part of my code and the error I receive in the output when the connection is reset.

import gevent.monkey
gevent.monkey.patch_all()

from requests import Session
from signalr import Connection

session=Session()
connection = Connection(server_url, session)
connection.qs.update({"token" : token})
connection.received += pushMessage
connection.error += print_error
hub = connection.register_hub('myhub')
connection.start()

# rest of the code
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 854, in gevent._gevent_cgreenlet.Greenlet.run
  File "/usr/local/lib/python3.5/dist-packages/signalr/_connection.py", line 53, in wrapped_listener
    listener()
  File "/usr/local/lib/python3.5/dist-packages/signalr/transports/_ws_transport.py", line 42, in _receive
    for notification in self.ws:
  File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 109, in __iter__
    yield self.recv()
  File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 314, in recv
    opcode, data = self.recv_data()
  File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 331, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 344, in recv_data_frame
    frame = self.recv_frame()
  File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 378, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/usr/local/lib/python3.5/dist-packages/websocket/_abnf.py", line 361, in recv_frame
    self.recv_header()
  File "/usr/local/lib/python3.5/dist-packages/websocket/_abnf.py", line 309, in recv_header
    header = self.recv_strict(2)
  File "/usr/local/lib/python3.5/dist-packages/websocket/_abnf.py", line 396, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/usr/local/lib/python3.5/dist-packages/websocket/_core.py", line 453, in _recv
    return recv(self.sock, bufsize)
  File "/usr/local/lib/python3.5/dist-packages/websocket/_socket.py", line 102, in recv
    bytes_ = _recv()
  File "/usr/local/lib/python3.5/dist-packages/websocket/_socket.py", line 84, in _recv
    return sock.recv(bufsize)
  File "/usr/local/lib/python3.5/dist-packages/gevent/_ssl3.py", line 554, in recv
    return self.read(buflen)
  File "/usr/local/lib/python3.5/dist-packages/gevent/_ssl3.py", line 385, in read
    return self._sslobj.read(nbytes or 1024)
  File "/usr/lib/python3.5/ssl.py", line 577, in read
    v = self._sslobj.read(len)
ConnectionResetError: [Errno 104] Connection reset by peer
2022-05-14T05:32:02Z <Greenlet at 0x7f53a9e6a748: wrapped_listener> failed with ConnectionResetError


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source