From 332c3076c0f911dba219ce8bb401e8f1b488c287 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Wed, 21 Feb 2024 21:34:37 +0100 Subject: [PATCH] Updated readme --- README.md | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fd2f461..9778741 100644 --- a/README.md +++ b/README.md @@ -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