Files
jianfeng-server/app/im/controller/Ws.php
2025-10-02 10:33:06 +08:00

197 lines
4.6 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\im\controller;
use app\im\controller\MessageHandlerV2;
use app\material\model\BrochureIm;
use Exception;
use Swoole\WebSocket\Server;
use think\App;
use think\facade\Config;
use think\session\driver\Cache;
class Ws {
public $ws = null;
public $server;
public $redis;
public $key;
public $messageHandler;
public function __construct() {
// $this->key = '1223454461';
// 加载框架里面的文件
require __DIR__ . '/../../../vendor/autoload.php';
$http = ( new App() )->http;
$response = $http->run();
$this->ws = new Server("0.0.0.0", Config::get('swoole.server.mport'));
$this->ws->set([
//worker进程数
'worker_num' => 4,
//task进程数
'task_worker_num' => 4,
//listen backlog
'backlog' => 128,
'max_request' => 50,
//心跳检测 每隔多少秒,遍历一遍所有的连接
'heartbeat_check_interval' => 30,
//心跳检测 最大闲置时间超时触发close并关闭
'heartbeat_idle_time' => 65,
// 总开关用来开启tcp_keepalive
'open_tcp_keepalive' => 1,
// 4s没有数据传输就进行检测
'tcp_keepidle' => 10,
// 4s没有数据传输就进行检测
'tcp_keepcount' => 10,
// 1s探测一次即每隔1s给客户端发一个包然后客户端可能会回一个ack的包如果服务端收到了这个ack包那么说明这个连接是活着的'tcp_keepcount' => 5,
// 探测的次数超过5次后客户端还没有回ack包那么close此连接
'tcp_keepinterval' => 5,
'log_file' => '/www/wwwroot/swoole.log',
'daemonize' => 1
]);
$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->on('workerStart' ,[$this ,'onWorkerStart']);
$this->ws->start();
}
public function onWorkerStart()
{
$this->messageHandler = new Some();
}
/**
* @param $ws
* @param $request
* @功能说明:建立连接回调
* @author chenniang
* @DataTime: 2021-07-02 18:17
*/
public function onOpen($ws, $request) {
$request->get['action'] = !empty($request->get['action'])?$request->get['action']:'mOnOpen';
if(method_exists($this->messageHandler, $request->get['action']))
{
// try{
call_user_func([$this->messageHandler, $request->get['action']] , $request->get,$request->fd);
// }catch(Exception $e) {
//
// }
}
// echo FILE_UPLOAD_PATH;
}
/**
* @param $ws
* @param $frame
* @功能说明:接受消息回调
* @author chenniang
* @DataTime: 2021-07-02 18:17
*/
public function onMessage($ws, $frame) {
$data = @json_decode($frame->data,true);
if(is_array($data)){
if(method_exists($this->messageHandler, $data['action']))
{
// try{
call_user_func([$this->messageHandler, $data['action']] ,$ws,$data,$frame->fd);
// }catch(Exception $e) {
//
// }
}
}
}
/**
* @param $serv
* @param $task_id
* @param $worker_id
* @param $data
* @功能说明:完成异步任务回调
* @author chenniang
* @DataTime: 2021-07-02 18:17
*/
public function onTask($serv, $task_id, $worker_id, $data) {
//返回字符串给worker进程——>触发onFinish
return "success";
}
/**
* @param $serv
* @param $task_id
* @param $data
* @功能说明:完成任务投递回调
* @author chenniang
* @DataTime: 2021-07-02 18:16
*/
public function onFinish($serv, $task_id, $data) {
//task_worker进程将任务处理结果发送给worker进程
echo "完成任务{$task_id}投递 处理结果:{$data}";
}
/**
* @param $ws
* @param $fd
* @功能说明:关闭连接回调
* @author chenniang
* @DataTime: 2021-07-02 18:16
*/
public function onClose($ws, $fd) {
// try{
call_user_func([$this->messageHandler, 'loginOut'] ,$ws, $fd);
// }catch(Exception $e) {
//
// }
// echo "{$fd}关闭了连接1";
}
}
$obj = new Ws();