Swoole 笔记
安装
GitHub Swoole
Step1: 下载最新的源码, 解压
Step2: 切换到源码目录, 执行 phpize
Step3: 切换到源码目录, 执行 ./configure
Step4: 切换到源码目录, 执行 make
Step5: 切换到源码目录, 执行 sudo make install
Step6: 修改 php.ini
文件, 文件尾部追加 extension=swoole
Hello World
服务端
新建 websocket_server_start.php
, 内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $ws = new Swoole\WebSocket\Server('0.0.0.0', 9502);
$ws->on('Open', function ($ws, $request) { $ws->push($request->fd, "hello, welcome\n"); });
$ws->on('Message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); });
$ws->on('Close', function ($ws, $fd) { echo "client-{$fd} is closed\n"; });
$ws->start();
|
使用命令 php websocket_server_start.php
启动服务
客户端(JS)
1 2 3 4 5 6 7 8 9 10 11 12 13
| ws = new WebSocket("ws://192.168.75.130:9502"); ws.onopen = function(event){ console.log(event); }; ws.onmessage = function (event) { console.log(event); var msg = "<p>"+event.data+"</p>"; $("#msgArea").append(msg); } ws.onclose = function(event){alert("已经与服务器断开连接\r\n当前连接状态:"+this.readyState);};
ws.onerror = function(event){console.log("WebSocket异常!");};
|
多人聊天室
服务端
通过 $server->connections
获取所有websocket连接用户的fd,进而实现群发功能
1 2 3 4 5 6
| foreach ($server->connections as $fd) { if ($server->isEstablished($fd)) { $server->push($fd, $request->get['message']); } }
|
客户端