Yii2-swoole-websocket extension, support json-rpc

Swoole is a PHP async network engine for production environment, Written by pure C language, Use for high performance TCP、UDP、Unix Socket、HTTP,WebSocket …

Yii2-swoole-websocket is a extension of Yii2 base on swoole.

Coding:

1, Create Controller under websocket/controllers, or other path which defined with “controllerNamespace” in websocket/config/main.php

<?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)
     }
    
     //...
}

2, Send RPC JSON to trigger that action

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

Example:

Chat room demo, code: ./example/websocket/controllers/RoomController.php

client join room:
websocket client send:

  {
      "jsonrpc":"2.0",
      "id":1,
      "method":"room/join",
      "params":{
          "id":"100111",
          "info":{
              "age":"19",
              "gender":"f"
          }
      }
  }

the websocket client which had joined same room (id:100111) will get message like this:

  {
      "jsonrpc":"2.0",
      "id":1,
      "result":{
          "type":"join",
          "count":85,
          "info":{
              "age":"19",
              "gender":"f"
          }
      }
  }

chat message
websocket client send:

    {
        "jsonrpc":"2.0",
        "id":1,
        "method":"room/msg",
        "params":{
            "id":"100111",
            "content":{
                "text":"Hello world!"
            }
        }
    }

this room member will get:

    {
        "jsonrpc":"2.0",
        "id":1,
        "result":{
            "text":"Hello world!"
        }
    }

All of client to server rpc command also can send by HTTP or Redis publish, This feature will helpful for some async task triggered from web application. Example in chat room case:

HTTP request:

http://127.0.0.1:8721/rpc?p={"jsonrpc":"2.0","id":1,"method":"room/msg","params":{"id":"100111","content":{"text":"System warning!"}}}

OR redis-cli:

127.0.0.1:6379> publish rpc '{"jsonrpc":"2.0","id":1,"method":"room/msg","params":{"id":"100111","content":{"text":"System warning!"}}}'

OR in Yii web application

Yii:$app->redis->publish('rpc', '{"jsonrpc":"2.0","id":1,"method":"room/msg","params":{"id":"100111","content":{"text":"System warning!"}}}')

OR use Hook (recommend), Support ‘runOnce’ to keep method run only once even if multiple swoole instances online, @see immusen/yii2-swoole-websocket/Hook.php

Yii::$app->hook->run('room/msg', ['id' => 100111, 'content' => ['text' => 'System warning!']]);
Yii::$app->hook->runOnce('sms/send', ['mobile' => 15600008721, 'code' => '8721']);
1 Like
  • More args support for portal shell
    ./websocket-server start/restart/reload/stop/status
    e.g.
./websocket-server status
master_pid=7404
start_time=1554030075
connection_num=130
accept_count=1025767
close_count=1025637
tasking_num=123
request_count=2051456
worker_request_count=511103
coroutine_num=2