swoole websocket服务器
服务器端环境
系统环境
uUbuntu 20.04
php版本
PHP 7.4.3
服务文件 ws_server.php
on(‘Open‘, function ($ws, $request) {
$ws->push($request->fd, "hello, websocket\n");
});
//监听WebSocket消息事件
$ws->on(‘Message‘, function ($ws, $frame) {
echo "Message: {$frame->data}\n";
$ws->push($frame->fd, "server: {$frame->data}");
});
//监听WebSocket连接关闭事件
$ws->on(‘Close‘, function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();
启动服务
php ws_server.php
测试
测试文件
vi web_socket.html
WebSocket测试
请按 F12 打开控制台查看消息
浏览器web_socket.html
请按 F12 打开控制台查看消息
注意事项:
1 检测防火墙是否放开9501端口
2 云服务器要开发9501端口
3 server.php监听的IP 0.0.0.0
原文:https://www.cnblogs.com/hu308830232/p/15125546.html