日韩在线观看-日韩在线成人-日韩在线不卡视频-日韩在线不卡视频-国产精品99-国产精品99

Socket 控制器

組件安裝

composer require easyswoole/socket

Examples

關(guān)于 Socket 控制器使用的具體示例,請(qǐng)查看 demo

包解析與控制器邏輯

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

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

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

        // 實(shí)現(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ù)的打包與響應(yīng)

對(duì)于數(shù)據(jù)的打包,開發(fā)者可以通過實(shí)現(xiàn) \EasySwoole\Socket\AbstractInterface\ParserInterface 接口的來實(shí)現(xiàn),然后在 decode 方法中實(shí)現(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
    {
        // 實(shí)現(xiàn)對(duì)數(shù)據(jù)的打包
        return pack('N', strlen(strval($response->getMessage()))) . $response->getMessage();
    }
}

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

<?php
namespace App\TcpController;

use EasySwoole\Socket\AbstractInterface\Controller;

class Index extends Controller
{
    public function index()
    {
        // 這里我們響應(yīng)一個(gè)字符串'this is index'給調(diào)用端
        $this->response()->setMessage('this is index');
    }
}
主站蜘蛛池模板: 时尚购物| 郑乙永| 电影因果报应完整版观看| 1—42集分集剧情简介| 妹妹扮演的角色| 情侣不雅视频| 邯郸恋家网| free hd xxxx moms movie777| 金时厚| 申请采购的请示范文| 纳西三部曲| 母乳妈妈忌口胀气食物| 日韩成人av电影77777| 川岛丽| 龙虎少年队2| 怡红院成人影院| 秀人网xiuren大尺度| cctv17农业农村频道在线直播| 二年级拍手歌生字组词| 戚薇床戏| 熊出没之重启未来| cetv3中国教育电视台直播| 真的爱你最标准谐音歌词| 女同恋性吃奶舌吻完整版| 郭麒麟个人资料简介| 大珍珠演员表介绍| 又造句二年级上册| 艳妇乳肉豪妇荡乳ⅹxxo| kaylani lei| 电影双面情人| 欧美日韩欧美| 画江湖之不良人第一季动漫免费观看 | 二年级aab词语| 谢锐韬个人资料| 欧美艹| 四年级上册第七课的生字拼音| 挠vk| 电影事物的秘密| 哈尔的移动城堡免费完整版| 韩国女主播热舞视频| 2014春节联欢晚会|