This commit is contained in:
jeremygan2021
2026-03-02 22:58:02 +08:00
parent 4c51f52654
commit 124b185b8a
7 changed files with 297 additions and 191 deletions

View File

@@ -10,6 +10,7 @@ class WebSocketClient:
self.sock = None
self.uri = uri
self.timeout = timeout
self.unread_messages = [] # Queue for buffered messages
self.connect()
def connect(self):
@@ -109,6 +110,13 @@ class WebSocketClient:
self.sock.write(masked_data)
def recv(self):
# 1. Check if we have unread messages in the buffer
if self.unread_messages:
return self.unread_messages.pop(0)
if not self.sock:
return None
# Read header
try:
# Read 2 bytes at once
@@ -176,3 +184,4 @@ class WebSocketClient:
if self.sock:
self.sock.close()
self.sock = None
self.unread_messages = []