利用了swoole监听其他的端口9502实现对task的任务分发
swoole的代码
<?php
namespace app\common;
include 'Predis.php';
include 'Task.php';
/**
* socket面向对象的编译
*/
class Ws
{
CONST HOST='0.0.0.0';
CONST PORT='9501';
public $ws=null;
public $getmsg=null;
public $server=null;
public function __construct()
{
$this->ws=new \swoole_websocket_server(self::HOST,self::PORT);
$this->ws->set([
//启动task必须要设置其数量
'worker_num' => 4,
'task_worker_num' => 2,
]);
//监听新端口
$this->server=$this->ws->listen("127.0.0.1", 9502, SWOOLE_SOCK_TCP);
//关闭websocket模式
$this->server->set([
'open_websocket_protocol' => false,
]);
$this->ws->on("start", [$this, 'onStart']);
$this->ws->on('open',[$this,'onopen']);
$this->server->on("receive", [$this, 'onReceive']);
$this->ws->on('task',[$this,'onTask']);
$this->ws->on('finish',[$this,'onFinish']);
$this->ws->on('message',[$this,'onmessage']);
$this->ws->on('close',[$this,'onclose']);
$this->server->on("close", [$this, 'onclose']);
$this->ws->start();
}
//监听数据接收事件
public function onReceive($serv, $fd, $from_id, $data)
{
$shuju=json_decode($data,ture);
// print_r($shuju).PHP_EOL;
if (empty($shuju['data'])) {
$this->ws->push(Predis::getInstance()->get('fd'), $data);
}else{
//执行异步任务
$this->ws->task($shuju);
// $this->ws->push(Predis::getInstance()->get('fd'), '异步任务开始了');
}
}
/**
* 设置进程名,为后续平滑重启

最低0.47元/天 解锁文章
998

被折叠的 条评论
为什么被折叠?



