Closed nskalis closed 3 years ago
Looks like you are always returning early from handling the messages, and then skip (loose) the remaining events with the other messages:
def handle_events(ws_conn):
for event in ws_conn.events():
if isinstance(event, AcceptConnection):
message = None
return (True, message) <-- DON'T RETURN HERE
...
thanks Thomas, I think I got it now. :)
Hi,
and thank you for making this library available.
I am facing a difficulty in consuming messages using
wsproto
. More specifically,on server-side 10 messages are sent one after the other:
fastapi
/starlette
running underuvicorn
using thewsproto
library, if it matters)def net_send(out_data, sock): sock.send(out_data)
def net_recv(ws_conn, sock): in_data = sock.recv(4096) if not in_data: ws_conn.receive_data(None) else: ws_conn.receive_data(in_data)
def handle_events(ws_conn): for event in ws_conn.events(): if isinstance(event, AcceptConnection): message = None return (True, message) elif isinstance(event, RejectConnection): message = {"error": event.status_code, "reason": "rejecting connection"} return (False, message) elif isinstance(event, CloseConnection): message = {"error": event.code, "reason": event.reason} sock.send(ws_conn.send(event.response())) return (False, message) elif isinstance(event, TextMessage): message = event.data return (True, message) else: message = {"error": -1, "reason": str(event)} return (False, message)
def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((API_IPADDR, API_PORT)) ws_conn = WSConnection(ConnectionType.CLIENT) cliend_id = str(uuid.uuid4()) endpoint = "/ws/{}".format(cliend_id) net_send(ws_conn.send(Request(host=API_IPADDR, target=endpoint)), sock) net_recv(ws_conn, sock) result, message = handle_events(ws_conn) if not result: print(message) # note: error
return
{"part": "data", "data": "xxx"} ^C--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last)