Skip to content

Commit eaef895

Browse files
committed
updated README
1 parent e89395e commit eaef895

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ Parameters:
463463

464464
uHTTP supports WebSocket connections (RFC 6455) in both event mode and non-event mode.
465465

466+
**Important:** When using `process_events()` with external select loop, `event_mode=True` is required for WebSocket support.
467+
466468
### Event Mode (non-blocking, multiple connections)
467469

468470
```python
@@ -487,17 +489,20 @@ while True:
487489
client.respond({'status': 'ok'})
488490

489491
elif client.event == EVENT_WS_MESSAGE:
490-
# str for text frames, bytes for binary frames
491-
client.ws_send(client.ws_message) # echo
492+
data = client.read_buffer() # bytes
493+
if client.ws_is_text:
494+
client.ws_send(data.decode('utf-8')) # echo as text
495+
else:
496+
client.ws_send(data) # echo as binary
492497

493498
elif client.event == EVENT_WS_CHUNK_FIRST:
494-
client.context = {'chunks': [client.ws_message]}
499+
client.context = {'chunks': [client.read_buffer()]}
495500

496501
elif client.event == EVENT_WS_CHUNK_NEXT:
497-
client.context['chunks'].append(client.ws_message)
502+
client.context['chunks'].append(client.read_buffer())
498503

499504
elif client.event == EVENT_WS_CHUNK_LAST:
500-
client.context['chunks'].append(client.ws_message)
505+
client.context['chunks'].append(client.read_buffer())
501506
# process all chunks...
502507
client.ws_send('received')
503508

@@ -532,10 +537,12 @@ while True:
532537
**HttpConnection properties:**
533538
- `is_websocket_request` - True if request is a WebSocket upgrade
534539
- `is_websocket` - True if connection is in WebSocket mode
535-
- `ws_message` - Last received message (str for text, bytes for binary)
540+
- `ws_is_text` - True if current message is text frame (vs binary)
541+
- `ws_message` - Ping/close payload (only for `EVENT_WS_PING` and `EVENT_WS_CLOSE`)
536542

537543
**HttpConnection methods (event mode):**
538544
- `accept_websocket()` - Accept upgrade, switch to WebSocket mode
545+
- `read_buffer()` - Read message data (returns bytes, use `ws_is_text` to check type)
539546
- `ws_send(data)` - Send message (str → text frame, bytes → binary frame)
540547
- `ws_ping(data=b'')` - Send ping frame
541548
- `ws_close(code=1000, reason='')` - Close WebSocket connection
@@ -549,7 +556,7 @@ while True:
549556

550557
### Large Message Chunking
551558

552-
Messages larger than `MAX_WS_MESSAGE_LENGTH` (default 64KB, configurable via `max_ws_message_length` kwarg) are delivered in chunks via `EVENT_WS_CHUNK_FIRST`, `EVENT_WS_CHUNK_NEXT`, `EVENT_WS_CHUNK_LAST` events. All chunk events contain data in `ws_message`.
559+
Messages larger than `MAX_WS_MESSAGE_LENGTH` (default 64KB, configurable via `max_ws_message_length` kwarg) are delivered in chunks via `EVENT_WS_CHUNK_FIRST`, `EVENT_WS_CHUNK_NEXT`, `EVENT_WS_CHUNK_LAST` events. Read chunk data with `read_buffer()`, check frame type with `ws_is_text`.
553560

554561
In non-event mode, messages exceeding the limit close the connection with status 1009.
555562

0 commit comments

Comments
 (0)