websocket - w.php
on('Open', function ($ws, $request) {
$ws->push($request->fd, "hello, welcome\n");
$GLOBALS['fds'][$request->fd] = 1;
print_r($GLOBALS['fds']);
});
//监听WebSocket消息事件
$ws->on('Message', function ($ws, $frame) {
echo "Message: {$frame->data}\n";
// $ws->push($frame->fd, "server: {$frame->data}");
$fds = $GLOBALS['fds'];
unset($fds[$frame->fd]);
foreach ($fds as $key => $value) {
$ws->push($key, substr($frame->data,4));
}
});
//监听WebSocket连接关闭事件
$ws->on('Close', function ($ws, $fd) {
unset($GLOBALS['fds'][$fd]);
echo "client-{$fd} is closed\n";
});
$ws->start();
浙公网安备 33010602011771号