inhere / websocket
一个轻量级的WebSocket应用程序库、服务器和客户端,由sockets、streams、swoole实现
0.5.1
2017-04-29 16:53 UTC
Requires
- php: >=7.0.0
- inhere/console: dev-master
- inhere/library: dev-master
Suggests
- inhere/php-validate: Very lightweight data validate tool
- inhere/simple-print-tool: Very lightweight data printing tools
This package is auto-updated.
Last update: 2024-09-20 00:56:26 UTC
README
一个轻量级的WebSocket应用程序库
安装
在 composer.json
中添加
"inhere/websocket": "dev-master",
运行: composer up
使用
仅使用WebSocket服务器
use Inhere\WebSocket\WebSocketServer; $ws = new WebSocketServer(); $ws->on('open', function (WebSocketServer $ws, $data) { $ws->send('welcome!'); }); $ws->on(WebSocketServer::ON_MESSAGE, function (WebSocketServer $ws, $data) { $ws->send("you input: $data"); }); $ws->start();
使用WebSocket应用程序
use Inhere\WebSocket\Application; use Inhere\WebSocket\WebSocketServer; $app = new Application('', 9501); $app->onOpen(function (WebSocketServer $ws, Application $app, $id) { $app->respond([ 'total' => $ws->count() ], 'welcome!'); }); $app->onClose(function (WebSocketServer $ws, Application $app) { $app->respond([ 'total' => $ws->count() ]); }); $rootHandler = $app->route('/', new \Inhere\WebSocket\handlers\RootHandler()); // commands $rootHandler->add('test', function ($data, $index, Application $app) { return 'hello'; });
WebSocket头部示例
WebSocket请求头部
// parsed code
GET / HTTP/1.1
Host: 127.0.0.1:9501
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: https://127.0.0.1:63342
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3018.3 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: zh-CN,zh;q=0.8
Cookie: _ga=GA1.1.1542925283.1469426767
Sec-WebSocket-Key: Tak3+4p37S5EAltSHDxpTw==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
// source code
GET ws://127.0.0.1:9501/ HTTP/1.1
Host: 127.0.0.1:9501
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: https://127.0.0.1:63342
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3018.3 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: zh-CN,zh;q=0.8
Cookie: _ga=GA1.1.1542925283.1469426767
Sec-WebSocket-Key: BmMdv63hr1D/eS7eTD59Vw==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
WebSocket响应头部(握手成功)
// parsed code
Connection:Upgrade
Sec-WebSocket-Accept:BOVf/XCi92SSib4Ga+ltTsmHiWQ=
Upgrade:websocket
// source code
HTTP/1.1 101 Switching Protocol
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: BOVf/XCi92SSib4Ga+ltTsmHiWQ=
许可证
MIT