Files
admin/module/Application/src/Command/Swoole/Server/SwItemTipCommand.php
2025-09-13 01:22:15 +08:00

55 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* @files.saveConflictResolution: overwriteFileOnDisk
* @Author: llbjj
* @Date: 2022-07-05 08:26:57
* @LastEditTime: 2022-07-05 14:16:46
* @LastEditors: llbjj
* @Description:
* @FilePath: /RemoteWorking/module/Application/src/Command/Swoole/Server/SwItemTipCommand.php
*/
namespace Application\Command\Swoole\Server;
use Application\Command\BasicCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SwItemTipCommand extends BasicCommand {
protected function execute(InputInterface $input, OutputInterface $output)
{
// 开启websock服务
$wsSv = new \Swoole\WebSocket\Server('0.0.0.0', 9590);
// 监听WebSocket连接打开事件
$wsSv->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;
}
}