hyperf中如何使用Swoole\Table
本篇文章给大家分享的是有关hyperf中如何使用Swoole\Table,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、网站建设、外贸网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的柳林网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
Swoole\Table的create需要在workStart之前,所以tcp服务启动之前,在server.php中配置SwooleEvent::ON_BEFORE_START监听事件
[ 'name' => 'tcp', 'type' => Server::SERVER_BASE, 'host' => '0.0.0.0', 'port' => 9503, 'sock_type' => SWOOLE_SOCK_TCP, 'callbacks' => [ SwooleEvent::ON_BEFORE_START => [\App\Tcp\ServerStartCallback::class, 'beforeStart'], SwooleEvent::ON_WORKER_START => [\App\Tcp\TcpServer::class, 'onWorkerStart'], SwooleEvent::ON_CONNECT => [\App\Tcp\TcpServer::class, 'onConnect'], SwooleEvent::ON_RECEIVE => [\App\Tcp\TcpServer::class, 'onReceive'], ] ]
在ServerStartCallback中实现Swoole\Table的初始化
column('fd', Table::TYPE_INT);
$table->column('reactor_id', Table::TYPE_INT);
$table->column('data', Table::TYPE_STRING, 64);
$table->create();
$container = ApplicationContext::getContainer();
$server = $container->get(Server::class);
$server->table = $table;
}
}在TCP建立连接接收消息的时候,进行fd的绑定
logger = $loggerFactory->get('log', 'default');
}
public function onWorkerStart(SwooleServer $server, int $worker_id): void
{
}
public function onConnect(SwooleServer $server, int $fd, int $fromId): void
{
$this->logger->debug($fd);
}
public function onReceive(SwooleServer $server, int $fd, int $fromId, string $data): void
{
$this->logger->debug($fd . ' - ' . $data);
// 检测数据,如果返回的前4位字符为IMEI,则应该为设备绑定fd
if (strpos($data, 'IMEI') === 0) {
$imei = substr($data, 5);
$server->table->set((string)$fd, [
'reactor_id' => $fromId,
'fd' => $fd,
'data' => $imei
]);
}
$this->logger->debug($server->table->count());
}
}以上就是hyperf中如何使用Swoole\Table,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
网站名称:hyperf中如何使用Swoole\Table
当前URL:http://www.jxjierui.cn/article/jpjpsc.html


咨询
建站咨询
