PHP code example of zxzgit / swoole-websocket-distributor

1. Go to this page and download the library: Download zxzgit/swoole-websocket-distributor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zxzgit / swoole-websocket-distributor example snippets


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

//#`php indexTest.php` to start a websocket server
\zxzgit\swd\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;
        },
    ]
]);