之前发布了第一个版本的群聊实现,大家可以去看一下上篇文章 swoole实现多对多群聊
这篇文章主要是在实现群聊的基础上加了日志功能以及代码的优化
老样子,直接上代码
PHP代码
<?php
define('HOST', '0.0.0.0');
define('PORT', '19501');
define('WORKER_NUM', 2);
define('TASK_WORKER_NUM', 4);
class WebsocktDemo
{
public $ws = null;
public $redis = null;
public $pdo = null;
public function __construct()
{
$this->ws = new Swoole\WebSocket\Server(HOST, PORT);
$this->redis = $this->redis();
$this->redis->auth("***********");
//实例化数据库链接
$this->pdo = new PDO(
"mysql:host=********;dbname=******;charset=utf8;",
"********",
"********",[\PDO::ATTR_CASE => \PDO::CASE_NATURAL]
);
//创建token找几个小弟
$this->ws->set([
'worker_num' => WORKER_NUM,
'task_worker_num' => TASK_WORKER_NUM,
]);
//$this->ws->on("start", [$this, 'onStart']);
$this->ws->on("open", [$this,