核心的swoole代码
基本的cs(client-sercer)结构不变,这里利用的是redis的哈希和set来储存和分组;从而达到了分组,统计,定时推送等功能;最后利用onclose事件来剔除断开的连接,全部代码如下:(没做前端,就不展示了)
核心的swoole ws.php
<?php
namespace app\common;
require_once 'Predis.php';
require_once '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,
// 'heartbeat_check_interval' => 5,
// 'heartbeat_idle_time' => 10,
]);
//监听新端口
$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, 'oncloses']);
$

最低0.47元/天 解锁文章
5614

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



