中文在线一区二区_欧美在线综合_久久久久久综合_欧美一区二区三区视频_国产免费看_国产福利精品一区

Socket 控制器

組件安裝

composer require easyswoole/socket

Examples

關于 Socket 控制器使用的具體示例,請查看 demo

包解析與控制器邏輯

數據解析與控制器映射

數據解析和控制器映射,開發者可以通過實現 \EasySwoole\Socket\AbstractInterface\ParserInterface 接口的來實現,然后在 encode 方法中實現數據解析和控制器映射。使用方法可以參考下面的示例。

下面以實現一個 tcp socket 控制器為例。首先定義協議解析器類 TcpParser 類,該類需要實現 \EasySwoole\Socket\AbstractInterface\ParserInterface 接口。如下:

<?php
namespace App\Parser;

use EasySwoole\Socket\AbstractInterface\ParserInterface;
use EasySwoole\Socket\Bean\Caller;

class TcpParser implements ParserInterface
{
    public function decode($raw, $client): ?Caller
    {
        // 數據解析,這里采用簡單的json格式作為應用層協議
        $data       = substr($raw, 4);
        $data       = json_decode($data, true);

        // 實現與控制器和action的映射
        $caller     = new Caller();
        $controller = !empty($data['controller']) ? $data['controller'] : 'Index';
        $action     = !empty($data['action']) ? $data['action'] : 'index';
        $param      = !empty($data['param']) ? $data['param'] : [];
        $controller = "App\\TcpController\\{$controller}";
        $caller->setControllerClass($controller);
        $caller->setAction($action);
        $caller->setArgs($param);
        return $caller;
    }

    // ... encode 方法
}

數據的打包與響應

對于數據的打包,開發者可以通過實現 \EasySwoole\Socket\AbstractInterface\ParserInterface 接口的來實現,然后在 decode 方法中實現數據的打包。使用方法可以參考下面的示例。

<?php
namespace App\Parser;

use EasySwoole\Socket\AbstractInterface\ParserInterface;
use EasySwoole\Socket\Bean\Response;

class TcpParser implements ParserInterface
{
    // ... decode 方法

    public function encode(Response $response, $client): ?string
    {
        // 實現對數據的打包
        return pack('N', strlen(strval($response->getMessage()))) . $response->getMessage();
    }
}

關于對數據的響應,則需要開發者在控制器的 action 進行處理,調用 $this->response()->setMessage($message) 進行響應調用端。參考示例如下:

<?php
namespace App\TcpController;

use EasySwoole\Socket\AbstractInterface\Controller;

class Index extends Controller
{
    public function index()
    {
        // 這里我們響應一個字符串'this is index'給調用端
        $this->response()->setMessage('this is index');
    }
}
主站蜘蛛池模板: 美国特级a毛片免费网站 | 精品久久久久久久久久久 | 最色网| 成人在线不卡 | 天天干天天操天天射 | 精品国产一级毛片 | 一色屋精品久久久久久久久久 | 综合自拍 | 亚洲精品无| 最近中文字幕 | 91av免费在线观看 | 国产精品99久久久久久动医院 | 欧美精品一区二区三区四区 | 久久成人国产 | 国产一区二区三区免费播放 | 欧美在线视频一区 | 毛片一区 | 亚洲视频二区 | 亚洲综合激情 | 国产在线拍揄自揄拍视频 | 99久久婷婷国产精品综合 | 高清一区二区三区 | 99国产精品99久久久久久 | 亚洲精品三级 | 国产激情精品视频 | 自拍偷拍亚洲欧美 | 国产午夜视频 | 最新中文字幕在线 | 日本中文一区二区 | 亚洲精品永久免费 | 精品久久久久久国产 | 亚洲电影一区二区 | 欧美精品第一页 | 3d动漫精品一区二区三区 | 91精品国产91久久久久久吃药 | 国产成人在线看 | 日本精品视频在线观看 | 亚洲欧美福利视频 | 欧洲亚洲精品久久久久 | 天堂网av在线| 日日日操 |