zxzgit/swoole-socket-distributor

There is no license information available for the latest version (v1.0) of this package.

v1.0 2019-01-24 01:43 UTC

This package is auto-updated.

Last update: 2024-04-26 12:29:30 UTC


README

基于 swoole 的 Swoole\Server 内容分发,可以将接受的信息分发到模块和控制器。

#基本用法

基本构建使用参考test目录:

--testapp
  --controllers
    --IndexController.php
    --OtherController.php
  --modules
    --test  //test module name
       --controllers
         --IndexController.php
         --OtherController.php
       --MessageModule.php
  --public
    --indexTest.php
  --MessageDistributor.php

testapp/public/indexTest.php

//#`php indexTest.php` to start a websocket server
\zxzgit\ssd\zxzgit\WebSocketApp::run([
    //'serverBind' => '0.0.0.0',//default 0.0.0.0
    //'serverPort' => '9502',//default serverPort
    'moduleList' => [
        'test' => \testapp\test\modules\test\MessageModule::class,
    ],
    'messageDistributor' => \testapp\test\MessageDistributor::class,
    'event' => [
        'initConnector' => function () {

        },
        'workerStart' => function (&$server, $id) {
            echo PHP_EOL . "workerStart event" . PHP_EOL;
        },
        'open' => function (&$server, &$req) {
            echo PHP_EOL . "open event" . PHP_EOL;
        },
        'beforeMessage' => function (&$server, &$frame) {
            echo PHP_EOL . "beforeMessage event" . PHP_EOL;
        },
        'afterMessage' => function (&$server, &$frame) {
            echo PHP_EOL . "afterMessage event" . PHP_EOL;
        },
        'close' => function (&$server, $fd) {
            echo PHP_EOL . "afterMessage event" . PHP_EOL;
        },
    ]
]);

testapp/controllers/IndexController.php

class IndexController extends \zxzgit\ssd\libs\AbstractController {
    //clien send json data {"route":"index/index","data":{"key1":"value1"}} can route to this action method
    public function actionIndex(){
        return $this->pushMsg(['hello', 'world']);
    }
}

testapp/MessageDistributor.php

class MessageDistributor extends \zxzgit\ssd\libs\MessageDistributor{
    public $moduleList = [
        //'test' => \zxzgit\ssd\test\modules\test\MessageModule::class,
    ];
}

testapp/modules/test/MessageModule.php

class MessageModule extends \zxzgit\ssd\libs\MessageModule{}