在线看片18_亚洲性吧_在线污_神马午夜达达兔_奇米色网_在线免费看污

Socket 控制器

組件安裝

composer require easyswoole/socket

Examples

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

包解析與控制器邏輯

數(shù)據(jù)解析與控制器映射

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

下面以實現(xiàn)一個 tcp socket 控制器為例。首先定義協(xié)議解析器類 TcpParser 類,該類需要實現(xiàn) \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
    {
        // 數(shù)據(jù)解析,這里采用簡單的json格式作為應用層協(xié)議
        $data       = substr($raw, 4);
        $data       = json_decode($data, true);

        // 實現(xiàn)與控制器和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 方法
}

數(shù)據(jù)的打包與響應

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

<?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
    {
        // 實現(xiàn)對數(shù)據(jù)的打包
        return pack('N', strlen(strval($response->getMessage()))) . $response->getMessage();
    }
}

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

<?php
namespace App\TcpController;

use EasySwoole\Socket\AbstractInterface\Controller;

class Index extends Controller
{
    public function index()
    {
        // 這里我們響應一個字符串'this is index'給調(diào)用端
        $this->response()->setMessage('this is index');
    }
}
主站蜘蛛池模板: 深爱开心激情 | 欧美无砖区 | 日本黄色高清视频 | 在线99视频 | 欧美色偷偷| 成人国产精品久久久网站 | 在线观看成人免费视频 | 亚洲+小说+欧美+激情+另类 | 亚洲日本色 | 亚洲美女网站 | 国产精品一二三区 | 精品欧美一区二区三区久久久 | 男人天堂2014| 国产一区二区三区影院 | 91丁香| 欧美黄色片网站 | 精品久久免费 | 亚洲精品www久久久久久 | 男人天堂网在线 | 国产69页| 精品国产一二三区 | 国产精品久久久久久久久久久久久久 | 蘑菇福利视频一区播放 | 欧美一卡二卡在线观看 | 亚洲精品久久久久 | 男人天堂最新网址 | 亚洲国产一级 | 国产精品九九视频 | 国产91丝袜美女在线播放 | 欧美大片一区二区三区 | 在线观看国产成人 | 精品国产aⅴ麻豆 | 免费av在线网站 | av婷婷在线 | 久久艹精品视频 | 欧美一区二区在线观看视频 | 一区二区视频在线 | 一区二区视 | 91久久精品一区二区三区 | 黄色小视频免费观看 | 中文字幕一区二区三区不卡 |