Updated readme

This commit is contained in:
Chris 2024-02-21 21:34:37 +01:00
parent 1caeb3c29f
commit 332c3076c0
1 changed files with 37 additions and 1 deletions

View File

@ -14,7 +14,15 @@ Ratchet is great! I've used Ratchet in the past, and it is a fantastic piece of
TL;DR - If you need to build an application with neatly wrapped classes without caring to much about the internals, go with Ratchet. If you want to work with websockets in the same way you work with sockets in ReactPHP, go with this library.
## Example
## Server
The WebSocket handler is built as a HttpServer middleware. This makes sense as WebSocket as a protocol is running over HTTP. Connections are set up by the middleware and exposed via the `connect` event.
Data is written and read as with any DuplexStream, use the write() method to send data to the client, and listen for the text and binary events on the connection.
Note that the write() method sends text frames, if you want to send a binary frame use writeBinary().
### Example
```php
// This is the middleware that will intercept WebSocket handshakes
@ -46,3 +54,31 @@ $socket = new React\Socket\SocketServer('0.0.0.0:8000');
$http->listen($socket);
```
### Server Events
#### connect
This event is emitted when a new WebSocket request has been accepted. The `WebSocketConnection` is passed as the first argument.
### WebSocketConnection events
#### ping
This event will be emitted upon receiving a frame with a ping opcode. The pong response has already been sent automatically, unless 'no_auto_pong' is set in the context.
#### pong
This event will be emitted upon receiving a frame with a pong opcode.
#### text
This event will be emitted when a text data frame have been received and decoded.
#### binary
This event will be emitted when a binary data frame have been received and decoded.
#### close
#### error