PHP code example of immusen / yii2-swoole-websocket

1. Go to this page and download the library: Download immusen/yii2-swoole-websocket 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/ */

    

immusen / yii2-swoole-websocket example snippets


php composer.phar 

vim ./websocket/config/params.php


namespace websocket\controllers;

use immusen\websocket\src\Controller;

class FooController extends Controller
{
     public function actionBar($param_1, $param_2 = 0, $param_n = null)
     {
          # add current fd into a group/set, make $param_1 or anyother string as the group/set key
          $this->joinGroup($this->fd, $param_1);
          
          # reply message to current client by websocket
          $this->publish($this->fd, ['p1' => $param_1, 'p2' => $param_2]);
          
          # get all fds stored in the group/set
          $fds_array = $this->groupMembers($param_1);
          
          # reply message to a group
          $this->publish($fds_array, ['p1' => $param_1, 'p2' => $param_2]);
          #or
          $this->sendToGroup(['p1' => $param_1, 'p2' => $param_2], $param_1);
          
          # operate redis via redis pool
          $this->redis->set($param_1, 0)
     }
    
     //...
}

    {
        "jsonrpc":"2.0",
        "id":1,
        "method":"foo/bar",
        "params":{
            "param_1":"client_01",
            "param_2":100,
            "param_n":{
                "time":1551408888,
                "type":"report"
            }
        }
    }