Swoole WebSocket服务端与客户端 持续更新
简单WebSocket 服务端实现
server.php
<?php
$server = new Swoole\WebSocket\Server("0.0.0.0", 9906);
//$server->set([
// 'enable_static_handler' => true, // 开启静态资源存在优先访问
// 'document_root' => './', // 静态资源目录
//]);
// 监听WebSocket链接打开事件
$server->on('open', 'onOpen');
function onOpen($server,$request){
print_r($request->fd);
}
// 监听WebSocket消息事件
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
简单WebSocket 客户端实现
client.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket测试</title>
</head>
<body>
<h1>WebSocket测试</h1>
<script>
var wsUrl = "ws://127.0.0.1:9906";
var webSocket = new WebSocket(wsUrl);
// 实例对象的onOpen属性
webSocket.onopen = function (e) {
webSocket.send("hello!");
console.log("打开链接成功!")
};
// 实例化 onMessage
webSocket.onmessage = function (e) {
console.log("接收的内容:",e.data);
};
// 实例化 onClose
webSocket.onclose = function (e) {
console.log("关闭链接");
};
// 实例化 onClose
webSocket.onerror = function (e) {
console.log("错误:",e.data);
};
</script>
</body>
</html>
访问
可以直接打开这个html页面 也可以直接在浏览器运行http://127.0.0.1:9906 不过前提是要打开服务端set的内容 并且能生效(因为笔者的不生效,所以选择第一种方法)
优化成面向对象
Ws.php
<?php
/**
* Created by PhpStorm.
* User: dom
* Date: 19-2-21
* Time: 上午11:51
*/
class Ws
{
public $ws = NULL;
public function __construct($host, $port)
{
$this->ws = new swoole_websocket_server($host, $port);
$this->ws->set(array(
// 'worker_num' => 8,
// 'max_request' => 10000,
// 'max_conn' => 100000,
// 'dispatch_mode' => 2,
// 'debug_mode' => 0,
// 'daemonize' => false,
'task_worker_num' => 8,
'log_level' => SWOOLE_LOG_ERROR, // 日志等级 关闭开启debug
'trace_flags' => SWOOLE_TRACE_SERVER, // 日志等级 关闭开启debug
));
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("task", [$this, 'onTask']);
$this->ws->on("finish", [$this, 'onFinish']);
$this->ws->on("close", [$this, 'onClose']);
$this->ws->start();
}
/**
* 监听ws连接事件
* @author: ZhuBN
* Date: 19-2-21 上午11:59
*
* @param swoole_websocket_server $ws
* @param $request
*/
public function onOpen(swoole_websocket_server $ws, $request)
{
var_dump($request->fd);
// TODO 执行异步定时器 每隔2秒就打印
// if ($request->fd == 1) {
// $timer = swoole_timer_tick(2000, function ($timerId) {
// echo "执行定时器";
// });
// swoole_timer_clear($timer);
// }
}
/**
* 监听ws消息事件
* @author: ZhuBN
* Date: 19-2-21 上午11:58
*
* @param swoole_websocket_server $ws
* @param $frame
*/
public function onMessage(swoole_websocket_server $ws, $frame)
{
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
// TODO task 异步任务调用
// $data = [
// 'task' => 1,
// 'fd' => $frame->fd
// ];
// $ws->task($data);
// TODO 执行异步定时器 5秒后执行一次
// swoole_timer_after(5000,function () use($ws,$frame){
// $ws->push($frame->fd, "定时器给你发送信息啦!");
// });
$ws->push($frame->fd, "this is server");
}
/**
* 异步任务处理逻辑
* @author: ZhuBN
* Date: 19-2-21 下午2:14
*
* @param swoole_websocket_server $ws
* @param $taskId
* @param $workerId
* @param $data
*
* @return string
*/
public function onTask(swoole_websocket_server $ws, $taskId, $fromId, $data)
{
echo "--------Task---------\n";
print_r($data);
echo "---------------------\n";
sleep(5);
return $data;
}
/**
* 异步任务处理完成回调
* @author: ZhuBN
* Date: 19-2-21 下午2:18
*
* @param swoole_websocket_server $ws
* @param $taskId
* @param $returnData
*/
public function onFinish(swoole_websocket_server $ws, $taskId, $returnData)
{
echo "-------Finish--------\n";
echo "taskId:{$taskId}\n";
print_r($returnData);
echo "---------------------\n";
}
/**
* 注销
* @author: ZhuBN
* Date: 19-2-21 下午2:30
*
* @param swoole_websocket_server $ws
* @param $fd
*/
public function onClose(swoole_websocket_server $ws, $fd)
{
echo "client {$fd} closed\n";
}
}
$obj = new Ws('0.0.0.0', 9906);
其中 task 异步任务处理 可以返回给用户信息,并且同时执行任务,从而提高用户体验
task不只是可以在WebSocket中使用也可以在HttpServer、TCPServer中使用
其中 异步毫秒定时器 可以按照规则处理数据,从而提高异步进行数据查询之类的操作
- swoole_timer_after N秒后执行一次
- swoole_timer_tick N秒后执行,循环
- swoole_timer_clear 取消这个定时器