本篇文章給大家分享的是有關(guān)hyperf中如何使用Swoole\Table,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的柳林網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
Swoole\Table的create需要在workStart之前,所以tcp服務(wù)啟動(dòng)之前,在server.php中配置SwooleEvent::ON_BEFORE_START監(jiān)聽(tīng)事件
[ '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中實(shí)現(xiàn)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建立連接接收消息的時(shí)候,進(jìn)行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); // 檢測(cè)數(shù)據(jù),如果返回的前4位字符為IMEI,則應(yīng)該為設(shè)備綁定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,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。