swoole websocket服务

wesocket服务端

//$server = new Swoole\WebSocket\Server("0.0.0.0", 8814);
$server = new swoole_webSocket_server("0.0.0.0", 8814);
//监听websocket打开事件
$server->on('open', 'onOpen');
function onOpen($server,$request){
    print_r($request->fd);
}
//监听客户端传过来的消息事件
$server->on('message',function(Swoole\WebSocket\Server $server, $frame) {
    //向客户端连接推送数据 返回结果 成功true 否则 false
    //$frame->fd 客户端连接标识id
    $server->push($frame->fd, "websocket server:".date('Y-m-d H:i:s'));

});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();

前端js代码

 

你可能感兴趣的:(Swoole)