You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.8 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* WebSocket服务端类
* User: fantasticbin
* Date: 2020/1/15
*/
class Ws
{
const HOST = '0.0.0.0';
const PORT = 8080;
public $server = null;
public function __construct()
{
// 创建WebSocket服务器监听本机的所有地址并监听 8080 端口
$this->server = new Swoole\WebSocket\Server(self::HOST, self::PORT);
// 设置响应html文件模式以及定义路径
$this->server->set([
'worker_num' => 2,
'task_worker_num' => 2,
'enable_static_handler' => true,
'document_root' => dirname(dirname(__FILE__)) . '/Template'
]);
// 监听客户端握手成功
$this->server->on('Open', [$this, 'onOpen']);
// 监听客户端发送消息
$this->server->on('Message', [$this, 'onMessage']);
// 监听task任务执行
$this->server->on('Task', [$this, 'onTask']);
// 监听task任务完成
$this->server->on('Finish', [$this, 'onFinish']);
// 监听关闭
$this->server->on('Close', [$this, 'onClose']);
}
/**
* 监听客户端握手成功
* @param $ws Swoole\WebSocket\Server WebSocket对象
* @param $request object 请求对象
*/
public function onOpen($ws, $request)
{
echo 'A new websocket client is connected, this client id is ' . $request->fd . PHP_EOL;
// 每隔10秒执行定时器向服务端输出信息
Swoole\Timer::tick(10000, function ($timer_id) {
echo "waiting client send message. this timer id is " . $timer_id . PHP_EOL;
});
}
/**
* 监听客户端发送消息
* @param $ws Swoole\WebSocket\Server WebSocket对象
* @param $frame object 客户端数据对象
*/
public function onMessage($ws, $frame)
{
echo "Receive from {$frame->fd} : {$frame->data}, opcode : {$frame->opcode}, fin : {$frame->finish}" . PHP_EOL;
$data = [
'name' => 'neo',
'age' => 30
];
// 投递task任务
$ws->task($data);
// 延时执行定时器指定5秒后给客户端发送消息
Swoole\Timer::after(5000, function () use ($ws, $frame) {
echo '5s-timer-after.' . PHP_EOL;
$ws->push($frame->fd, 'message_after-push, this time is ' . date('Y-m-d H:i:s'));
});
// 给客户端立即推送消息
$ws->push($frame->fd, 'message-push-success, this time is ' . date('Y-m-d H:i:s'));
}
/**
* 监听task任务执行
* @param $serv Swoole\WebSocket\Server WebSocket对象
* @param $task_id int task任务ID
* @param $worker_id int worker进程ID
* @param $data array 传递的数据
* @return string 执行结果
*/
public function onTask($serv, $task_id, $worker_id, $data)
{
// 打印传递过来的数据
var_dump($data);
// 模拟耗时场景执行10秒
sleep(10);
// 告诉worker结果
return 'on task finish.';
}
/**
* 监听task任务完成
* @param $serv Swoole\WebSocket\Server WebSocket对象
* @param $task_id int task任务ID
* @param $data string onTask执行完返回的结果
*/
public function onFinish($serv, $task_id, $data)
{
echo "Finish task id: {$task_id}" . PHP_EOL;
echo "Finish data message: {$data}" . PHP_EOL;
}
/**
* 监听关闭
* @param $ws Swoole\WebSocket\Server WebSocket对象
* @param $client_id int 客户端连接ID
*/
public function onClose($ws, $client_id)
{
echo "client-{$client_id} is closed" . PHP_EOL;
}
/**
* 启动WebSocket服务器
*/
public function start()
{
$this->server->start();
}
}
$webSocket = new Ws();
$webSocket->start();