Updated example, readme
This commit is contained in:
parent
560e5f7881
commit
16fa77229c
17
README.md
17
README.md
@ -2,18 +2,12 @@
|
|||||||
|
|
||||||
## Installing
|
## Installing
|
||||||
|
|
||||||
Install using composer:
|
It is recommended that you use composer to install the package:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ composer require noccylabs/react-websocket
|
$ composer require noccylabs/react-websocket:^0.1.0
|
||||||
```
|
```
|
||||||
|
|
||||||
## Why not Ratchet?
|
|
||||||
|
|
||||||
Ratchet is great! I've used Ratchet in the past, and it is a fantastic piece of code. It is however more application-centered, which means it doesn't do events and all that beautiful magic we've come to love ReactPHP for.
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
## Missing Features
|
## Missing Features
|
||||||
|
|
||||||
The following features are missing, or work in progress:
|
The following features are missing, or work in progress:
|
||||||
@ -21,6 +15,13 @@ The following features are missing, or work in progress:
|
|||||||
* Idle timeout and ping timeout
|
* Idle timeout and ping timeout
|
||||||
* Protocol errors should close with error codes
|
* Protocol errors should close with error codes
|
||||||
* Exceptions
|
* Exceptions
|
||||||
|
* Full tested support for fragmented frames
|
||||||
|
|
||||||
|
## Why not Ratchet?
|
||||||
|
|
||||||
|
Ratchet is great! I've used Ratchet in the past, and it is a fantastic piece of code. It is however more application-centered, which means it doesn't do events and all that beautiful magic we've come to love ReactPHP for.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
## Server
|
## Server
|
||||||
|
|
||||||
|
@ -10,8 +10,14 @@ use React\Http\Message\Response;
|
|||||||
use React\Promise\Promise;
|
use React\Promise\Promise;
|
||||||
use React\Socket\SocketServer;
|
use React\Socket\SocketServer;
|
||||||
|
|
||||||
|
// The middleware is at the core, as it intercepts and breaks out the websocket
|
||||||
|
// connections received by the HttpServer.
|
||||||
$websockets = new WebSocketMiddleware();
|
$websockets = new WebSocketMiddleware();
|
||||||
|
// The 'connection' handler works like expected. You can't however write any data
|
||||||
|
// to the client yet, as the websocket streams have not yet been returned.
|
||||||
$websockets->on('connection', function (WebSocketInterface $websocket) {
|
$websockets->on('connection', function (WebSocketInterface $websocket) {
|
||||||
|
// This just echoes text received, unless the websocket is part of a group.
|
||||||
|
// In this case the message is sent to all websockets in the group.
|
||||||
$websocket->on('text', function ($text) use ($websocket) {
|
$websocket->on('text', function ($text) use ($websocket) {
|
||||||
if (str_starts_with($text, '#')) {
|
if (str_starts_with($text, '#')) {
|
||||||
$websocket->setGroup(substr($text,1));
|
$websocket->setGroup(substr($text,1));
|
||||||
@ -27,8 +33,10 @@ $websockets->on('connection', function (WebSocketInterface $websocket) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Create the server with our middleware chain:
|
||||||
|
// error handler -> websocket handler -> http handler
|
||||||
$server = new HttpServer(
|
$server = new HttpServer(
|
||||||
$websockets,
|
// Error handler
|
||||||
function (ServerRequestInterface $request, callable $next) {
|
function (ServerRequestInterface $request, callable $next) {
|
||||||
$promise = new Promise(function ($resolve) use ($next, $request) {
|
$promise = new Promise(function ($resolve) use ($next, $request) {
|
||||||
$resolve($next($request));
|
$resolve($next($request));
|
||||||
@ -39,11 +47,14 @@ $server = new HttpServer(
|
|||||||
)->withStatus(Response::STATUS_INTERNAL_SERVER_ERROR);
|
)->withStatus(Response::STATUS_INTERNAL_SERVER_ERROR);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
// WebSocket handler
|
||||||
|
$websockets,
|
||||||
|
// HTTP handler
|
||||||
function (ServerRequestInterface $request) {
|
function (ServerRequestInterface $request) {
|
||||||
return Response::plaintext("Hello world!");
|
return Response::plaintext("Hello world!");
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Everything else is as expected!
|
||||||
$socket = new SocketServer("tcp://0.0.0.0:8000");
|
$socket = new SocketServer("tcp://0.0.0.0:8000");
|
||||||
|
|
||||||
$server->listen($socket);
|
$server->listen($socket);
|
Loading…
Reference in New Issue
Block a user