WebSocket Controller

Change Main Server Type

vim your Config.php,and change SERVER_TYPE item into


Implement You Package Parser

you must implement a package parser which implement 'EasySwoole\Core\Socket\AbstractInterface\ParserInterface',in case easyswoole can understand the command from websocket client and response the correct data-form to it. For example:

suppose the data-form of client is a json,


so the parser is

namespace App;

use EasySwoole\Core\Socket\AbstractInterface\ParserInterface;
use EasySwoole\Core\Socket\Common\CommandBean;

class Parser implements ParserInterface

    public static function decode($raw, $client)
        // TODO: Implement decode() method.
        $command = new CommandBean();
        $json = json_decode($raw,1);
        //suppose all target controller is \App\WebSocket\Test::class
        return $command;


    // @$raw is a  string which create from SplStream what you response at your controller action
    public static function encode(string $raw, $client): ?string
        // TODO: Implement encode() method.
            return $raw;
            return null;

Register OnMessage Callback

vim EasySwooleEvent.php

use \EasySwoole\Core\Swoole\EventHelper;

public static function mainServerCreate(ServerManager $server,EventRegister $register): void
    // TODO: Implement mainServerCreate() method.

results matching ""

    No results matching ""