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();

posted on 2023-04-06 23:37  木林coder  阅读(27)  评论(0)    收藏  举报

导航