websockets.exceptions.ConnectionClosedError: no close frame received or sent
hsh4269 opened this issue · 6 comments
Hi, I am a beginner.
I'm asking because an error occurred randomly when connecting to the web socket.
The error message is
"websockets.exceptions.ConnectionClosedError: no close frame received or sent"
Bybit's API document recommends sending Heartbeat Packet every 20 seconds to maintain a websocket connection.
Like this
ws.send(JSON.stringify({"req_id": "100001", "op": "ping"}));
It looks like this, but I don't think that part is working.
How do I do Heartbeat Packet to maintain a websocket connection in pybit?
You don't need to take any proactive measure to send a heartbeat packet with pybit. It does it for you.
The code you mention is for javascript, not python.
So...... why does this happen?
I'm not able to reproduce this, can you send the code you used?
Process WebSocketManager-4:1:
Traceback (most recent call last):
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\websockets\legacy\protocol.py", line 974, in transfer_data
message = await self.read_message()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\websockets\legacy\protocol.py", line 1044, in read_message
frame = await self.read_data_frame(max_size=self.max_size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\websockets\legacy\protocol.py", line 1119, in read_data_frame
frame = await self.read_frame(max_size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\websockets\legacy\protocol.py", line 1176, in read_frame
frame = await Frame.read(
^^^^^^^^^^^^^^^^^
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\websockets\legacy\framing.py", line 69, in read
data = await reader(2)
^^^^^^^^^^^^^^^
File "c:\Program Files\Python311\Lib\asyncio\streams.py", line 726, in readexactly
raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes
The above exception was the direct cause of the following exception:
self.run()
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\pybithumb\websocket.py", line 71, in run
self.__aloop.run_until_complete(self.__connect_socket())
File "c:\Program Files\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\pybithumb\websocket.py", line 66, in __connect_socket
recv_data = await websocket.recv()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\websockets\legacy\protocol.py", line 568, in recv
await self.ensure_open()
File "C:\Users\HSH\AppData\Roaming\Python\Python311\site-packages\websockets\legacy\protocol.py", line 941, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: no close frame received or sent
The above error message is output.
What is the cause?
How can I solve this problem?
It looks like you're not using the pybit
library in this code. From the traceroute you're using the websockets
library, but pybit doesn't use this – it uses websocket-client
. So it looks like you're not using pybit here.
If you disagree, please send the code you used so that I can try reproducing this problem.
import time
import datetime as dt
import math
from multiprocessing import Process, Manager
import asyncio
from pybit.unified_trading import HTTP, WebSocket
######## Defines ###########################
testnet = False
pybit = HTTP(
testnet=testnet,
api_key="111", #api key
api_secret="111", #api secreat
logging_level = 10,
)
ws = WebSocket(
testnet=False,
channel_type="spot",
)
def handle_message(message):
global Bybit_Ask_0, Bybit_Bid_0, Bybit_Ask_Volume_0
try:
orderbook_data = message["data"]
Bybit_Ask_0 = float(orderbook_data['a'][0][0])
Bybit_Ask_Volume_0 = float(orderbook_data['a'][0][1])
Bybit_Bid_0 = float(orderbook_data['b'][0][0])
except Exception as e:
print("handle_message" , e, dt.datetime.now(), sep='\t')
pass
############################################################
def main():
proc1 = Process(name="P_XRP", target=sub_program, args=("XRP",))
proc1.start() #XRP
time.sleep(1)
proc1.join() #XRP
def sub_program(Set_Coin):
ws.orderbook_stream(1, 'XRPUSDT', handle_message)
time.sleep(3)
while True:
print(Bybit_Ask_0, Bybit_Ask_Volume_0)
if name == "main":
main()
This one is I used code.
How can I solve this problem?