xp-forge / websockets
WebSockets for the XP Framework
v4.0.0
2024-10-05 14:21 UTC
Requires
- php: >=7.4.0
- xp-forge/uri: ^2.0 | ^1.4
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
- xp-framework/logging: ^11.0 | ^10.0 | ^9.1
- xp-framework/networking: ^10.0 | ^9.3
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
Example
use websocket\Listeners; class Imitator extends Listeners { public function serve($events) { return [ '/echo' => function($conn, $payload) { $conn->send('You said: '.$payload); } ]; } }
Run it using:
$ xp -supervise ws Imitator @peer.ServerSocket(Resource id #138 -> tcp://0.0.0.0:8081)) Serving Imitator(dev)[] > websocket.logging.ToConsole # ...
To connect to this server, use the following:
use util\cmd\Console; use websocket\WebSocket; $s= new WebSocket('ws://localhost:8081/echo'; $s->connect(); $s->send('Hello'); Console::writeLine('<<< ', $s->receive());
On the JavaScript side, open the connection as follows:
var ws = new WebSocket('ws://localhost:8081/echo'); ws.onmessage = (event) => console.log(event.data); ws.send('Hello'); // Will log "You said: Hello" to the console