swoole利用task分发任务案例(群发短信)

利用了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'), '异步任务开始了');
        }
    }
    /**
     * 设置进程名,为后续平滑重启
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值