t4web / websocket
ZF2 Module. Websocket server
dev-master
2016-07-26 10:43 UTC
Requires
- php: ^5.5 || ^7.0
- cboden/ratchet: ~0.3.5
- ratchet/pawl: ^0.2.2
- react/socket: ~0.4.2
- t4web/default-service: ~0.1.0
- zendframework/zend-console: ~2.5.0
- zendframework/zend-json: ~2.5.0
- zendframework/zend-servicemanager: ~2.5.0
Requires (Dev)
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-10-26 18:12:13 UTC
README
ZF2 Module. Websocket server build on Ratchet
Instalation
Just require t4web/websocket
and add to your application.config.php
T4web\Websocket
.
Run
In console run
$ php public/index.php websocket start
Configuration
Add in your global.config.php
:
't4web-websocket' => [ 'server' => [ // required 'port' => 8088, // not required, default 0 'debug-enable' => 1, ], ];
Quick usage
For testing create html (or copy from https://github.com/t4web/Websocket/blob/master/ws-test.html):
<html lang="en"> <body> <input type="text" id="input" placeholder="Messageā¦" /> <hr /> <pre id="output"></pre> <script> var host = 'ws://127.0.0.1:8088'; var socket = null; var input = document.getElementById('input'); var output = document.getElementById('output'); var print = function (message) { var samp = document.createElement('samp'); samp.innerHTML = message + '\n'; output.appendChild(samp); return; }; input.addEventListener('keyup', function (evt) { if (13 !== evt.keyCode) { return; } var msg = { 'event' : 'ping', 'data': {message: input.value} }; if (!msg) { return; } try { print('Send: ' + JSON.stringify(msg, null, 4)); socket.send(JSON.stringify(msg)); input.value = ''; input.focus(); } catch (e) { console.log(e); } return; }); try { socket = new WebSocket(host); socket.onopen = function () { print('connection is opened'); input.focus(); return; }; socket.onmessage = function (msg) { print('Receive: ' + JSON.stringify(JSON.parse(msg.data), null, 4)); return; }; socket.onclose = function () { print('connection is closed'); return; }; } catch (e) { console.log(e); } </script> </body> </html>
Or from PHP:
/** @var \T4web\Websocket\WebsocketClient $wsClient */ $wsClient = $this->getServiceLocator()->get(\T4web\Websocket\Client::class); $wsClient->send('ping', ['message' => 'Hello World']);
Proposed architecture
Each websocket request message must be json object:
{ "event": "event-name", "data": { "field": "value" } }
Websocket response message:
{ "event": "event-name", "data": { "field": "value" }, "error": "string|null" }
Each websocket request mesage - you can handle. Just describe own handlers in t4web-websocket[event-handlers]
module.config.php
:
't4web-websocket' => [ 'event-handlers' => [ // event name => handler - Callable object 'ping' => Handler\Ping::class, ], ],
Handler\Ping
:
use SplObjectStorage; use Ratchet\ConnectionInterface; class Ping implements T4web\Websocket\Handler\HandlerInterface { public function handle($eventName, array $data, ConnectionInterface $connection, SplObjectStorage $connections) { $response = [ 'event' => 'pong', 'data' => $data, 'error' => null, ]; $connection->send(json_encode($response)); } }