全局$server推送数据,其他work进程连接收不到推送消息。

class WebSocketServer
 { 
  public $server; public function __construct() { 
    $this->server = new \swoole_websocket_server('0.0.0.0', 9502, SWOOLE_BASE, SWOOLE_SOCK_TCP); 
    $this->server->set([ 'worker_num' => 16, 'daemonize' => false, ]); 
    $this->server->on('Open', [$this, 'onOpen']); 
    $this->server->on('Message', [$this, 'onMessage']); 
    $this->server->on('Request', [$this, 'onRequest']); 
    $this->server->on('Close', [$this, 'onClose']); $this->server->start(); 
}

public function onOpen(\swoole_websocket_server $server, \swoole_http_request $request)
{      
    echo "服务端连接成功:{$request->fd}\n";
}

public function onRequest(\swoole_http_request $request, \swoole_http_response $response)
{
    $data=$request->post;
    $this->tui(json_encode($data));
}

public function tui($currency, $res)
{
    $redis=RedisConnect::getInstance();
    $fd_key='fd';
    $fds=$redis-
}
}
2018-10-31 15:53
0
0

Rango - 一枚职业的码农

赞同来自:

代码不完整,你需要使用 foreach($serv->connections as $fd) 向所有连接发送数据。也可以使用 Redis 保存所有fd

在你的代码中使用了SWOOLE_BASE,请认真阅读Server模式中的说明,BASE模式是无法跨进程发送消息的。有3种解决方案:

  • 请使用SWOOLE_PROCESS模式
  • 使用onPipeMessage实现进程间通信,互相转发消息
  • 将进程数worker_num设置为1
2018-11-27 22:09

要回复问题请先登录注册