hyperf中如何使用Swoole\Table

本篇文章給大家分享的是有關hyperf中如何使用Swoole\Table,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站設計、網(wǎng)站建設、外貿(mào)網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的柳林網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

Swoole\Table的create需要在workStart之前,所以tcp服務啟動之前,在server.php中配置SwooleEvent::ON_BEFORE_START監(jiān)聽事件

        [
            '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中實現(xiàn)Swoole\Table的初始化

<?php

namespace App\Tcp;

use Hyperf\Server\ServerInterface;
use Hyperf\Server\SwooleServerFactory;
use Hyperf\Utils\ApplicationContext;
use Swoole\Server;
use Swoole\Table;

class ServerStartCallback extends \Hyperf\Framework\Bootstrap\ServerStartCallback
{


    public function beforeStart()
    {
        $table = new Table(65536);
        $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的綁定

<?php
declare(strict_types = 1);

namespace App\Tcp;

use Hyperf\Contract\OnOpenInterface;
use Hyperf\Contract\OnReceiveInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Server\ServerFactory;
use Hyperf\Di\Annotation\Inject;
use PBurggraf\CRC\CRC16\CRC16;
use Psr\Log\LoggerInterface;
use Swoole\Http\Request;
use Swoole\Server as SwooleServer;
use Swoole\Table;
use Swoole\WebSocket\Server;
use Hyperf\Framework\Logger\StdoutLogger;

class TcpServer implements OnReceiveInterface
{
    /**
     * @Inject
     * @var CRC16
     */
    private $crc16;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        $this->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);
        // 檢測數(shù)據(jù),如果返回的前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,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文標題:hyperf中如何使用Swoole\Table
網(wǎng)站路徑:http://www.muchs.cn/article32/jpjpsc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站、網(wǎng)站策劃、網(wǎng)站改版、網(wǎng)站維護、服務器托管、面包屑導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設