/** * 全服广播 * @param [type] $data [description] * @return [type] [description] */ public function broadcast($data) { // 打包数据 $data = DataParser::encode($data); foreach ($server->connections as $fd) { $server->send($fd, $data); } }
/** * 全服广播 * @param [type] $data [description] * @return [type] [description] */ public function broadcast($data) { // 打包数据 $data = DataParser::encode($data); $start_fd = 0; while (true) { $conn_list = $this->serv->connection_list($start_fd, 10); if ($conn_list === false) { break; } $start_fd = end($conn_list); foreach ($conn_list as $fd) { $this->serv->send($fd, $data); } } }
/** * 接收数据回调 * @param \swoole_server $serv swoole_server 对象 * @param intval $fd TCP客户端连接的文件描述符 * @param intval $from_id TCP连接所在的Reactor线程ID * @param string $data 数据包 * @return null */ public function onReceive(\swoole_server $serv, $fd, $from_id, $data) { global $di; Console::info($data); // 解包数据 $data = DataParser::decode($data); // 注入fd $di->set('fd', function () use($fd) { return $fd; }); // 注入from_id $di->set('from_id', function () use($from_id) { return $from_id; }); // 注入request $di->set('request', function () use($data) { return $data['data']; }); // 数据分发 $router = \Swoole\Dispatcher::getInstance($di); $router->adapter($data['code']); }