php页面
//监听WebSocket连接打开事件
<?php
$ws = new swoole_websocket_server("0.0.0.0", 9503);
$ws->on('open', function ($ws, $request) {
// var_dump($request->fd, $request->get, $request->server);
$ws->push($request->fd, '与服务器连接成功,我是用户'.$request->fd);
});
//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
$data=$frame->data;
$data=json_decode($data,true);
foreach($ws->connections as $fd) {
if($fd!=$frame->fd) {
$ws->push($fd, $data['data']);
}
}
});
//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();
html 页面
<html><head>
<meta http-equiv="Content-Type" content="text/html; chars