前言
一直在做swoole的websocket,之前是利用swoole的client->send()来使用,后来一直想用redis的订阅模式来实现(其实早就有人做了),今天来实现以下方法.
直接上代码
socket.php的核心文件
<?php
use socket\Predis;
require 'Predis.php';
class jieshou
{
public $server = null;
public $client = null;
function __construct()
{
$this->server = new Swoole\WebSocket\Server("0.0.0.0", 10086);
$this->server->set([
//心跳检测
'heartbeat_check_interval' => 60,
'heartbeat_idle_time' => 600,
//设置证书,实现wss连接
// 'ssl_cert_file' => $this->pwd.'pai.wanguo.net.crt',
// 'ssl_key_file' => $this->pwd.'pai.wanguo.net.key',
]);
$this->server->on('workerStart', [$this, 'onworkerStart']);
$this->server->on('message', [$this, 'onmessage']);
$this->server->on('close', [$this, 'onclose']);
$this->server->start();
}
/**
* 发送全部联系人
* @param $name
*/
public function push_all($name, $json)
{
foreach ($this->server->connections as $fd) {
$this->server->push($fd, $json);
}
}
/**
* 发送指定联系人
* @param $member_id
* @param $json
*/
public function push_one($name, $member_id