<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # 1. 數據格式定義 暫定json數據格式定義為{"controller":"Im","action:"demo","param":{json數據}}(時間足夠后續可能會改用pubutuf) # 2. 數據測試 發送數據測試 如圖 收到數據后進行解碼以及轉發數據到對應控制器![](https://i.loli.net/2019/03/11/5c862bba7d442.png) # 3. 數據解碼 在message回調中實現解碼即可,代碼如下,并實現抽象接口ParserInterface ``` //解碼如下 $conf = new \EasySwoole\Socket\Config(); $conf->setType($conf::WEB_SOCKET); $conf->setParser(new WebSocketParser); $dispatch = new Dispatcher($conf); $register->set(EventRegister::onMessage, function (\swoole_server $server, \swoole_websocket_frame $frame) use ($dispatch) { $dispatch->dispatch($server, $frame->data, $frame); }); ``` 實現抽象接口代碼如下 ``` namespace App\WebSocket; use EasySwoole\Socket\AbstractInterface\ParserInterface; use EasySwoole\Socket\Bean\Caller; use EasySwoole\Socket\Bean\Response; use EasySwoole\Socket\Client\WebSocket as WebSocketClient; class WebSocketParser implements ParserInterface { /** * 解碼上來的消息 * @param string $raw 消息內容 * @param WebSocketClient $client 當前的客戶端 * @return Caller|null */ public function decode($raw, $client): ?Caller { $caller = new Caller; $payload = json_decode($raw, true); $class = isset($payload['controller']) ? $payload['controller'] : 'ImController'; $action = isset($payload['action']) ? $payload['action'] : 'index'; $params = isset($payload['params']) ? (array)$payload['params'] : []; $controllerClass = "\\App\\WsController\\" . ucfirst($class); if (!class_exists($controllerClass)) $controllerClass = "\\App\\WsController\\ImController"; $caller->setClient($caller); $caller->setControllerClass($controllerClass); $caller->setAction($action); $caller->setArgs($params); return $caller; } /** * 打包下發的消息 * @param Response $response 控制器返回的響應 * @param WebSocketClient $client 當前的客戶端 * @return string|null */ public function encode(Response $response, $client): ?string { return $response->getMessage(); } } ``` # 4. 測試解包后對應控制器方法是否能正常收發消息 創建ImController如圖 打開http://www.easyswoole.com/wstool.html連接ws后發送消息如圖所示 ![](https://i.loli.net/2019/03/14/5c8a0bb3e001a.png) ![](https://i.loli.net/2019/03/14/5c8a0c7a4934b.png) ImController代碼如下 ```<?php /** * Created by PhpStorm. * User: pg * Date: 19-3-14 * Time: 下午2:58 */ namespace App\Controller\Ws; use EasySwoole\Socket\AbstractInterface\Controller; class ImController extends Controller { public function index(){ $client = $this->caller()->getClient(); $origin_param['fd'] = $client->getFd(); $origin_param['again_time'] = time(); $origin_param['send_count'] = 3; $origin_param['msg'] = $this->caller()->getArgs(); $server = \EasySwoole\EasySwoole\ServerManager::getInstance()->getSwooleServer(); $server->push($client->getFd(),json_encode($origin_param)); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看