on('open', function(\Swoole\WebSocket\Server $server, $request) use($output) { // $output->writeln("server: handshake success with fd{$request->fd}".PHP_EOL); }); // 监听客户端消息事件 $wsSv->on('message', function(\Swoole\WebSocket\Server $server, $frame) use($output) { $output->writeln("receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}".PHP_EOL); }); // 利用swoole的定时器,定时请求数据实时推送到客户端;timer的简单用法 $addProcess = new \Swoole\Process( function($process) use($wsSv, $output) { \Swoole\Timer::tick(5000, function (int $timer_id, $wsSv) { foreach ($wsSv->connections as $fd){ // 根据实际情况获取数据,发送给客户端(目前只是测试数据) $wsSv->push($fd, $fd.":项目总数量:".$this->LocalService()->itemInfo->getCount().' 个 '.time()); } }, $wsSv); }); $wsSv->addProcess($addProcess); // 监听客户端断开链接 $wsSv->on('close', function($server, $fd) use($output) { $output->writeln("client {$fd} closed".PHP_EOL); }); // 启动websock服务 $wsSv->start(); return 0; } }