goodizer/yii2-websocket

Components for Yii2.

Installs: 618

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 2

Open Issues: 0

Type:yii2-components

v1.0.8 2020-07-18 21:57 UTC

This package is not auto-updated.

Last update: 2024-11-20 17:00:35 UTC


README

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads

Web-socket component based on workerman/workerman for Yii2

Installation

The preferred way to install this extension is through composer.

Note: Check the composer.json for this extension's requirements and dependencies.

Either run

$ php composer.phar require goodizer/yii2-websocket

or add

"goodizer/yii2-websocket": "*"

to the require section of your composer.json file.

Usage

GridSearchHelper

Set websocket component config.

    'components' => [
        ...
        'websocketServer' => [
          'class' => 'goodizer\websocket\Server',
          'commandClass' => 'console\extensions\Commands',//Your class that inherit goodizer\websocket\Commands
            'host' => $params['websocketServer']['host'] ?? 'localhost',
            'port' => $params['websocketServer']['port'] ?? 8000,
            'isSecure' => $params['websocketServer']['isSecure'] ?? false,
            'localCert' => $params['websocketServer']['localCert'] ?? null,
            'localPk' => $params['websocketServer']['localPk'] ?? null,
        ],
        ...
    ],

Create controller

<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use goodizer\websocket\Server;

/**
 * Class WebsocketServerController
 * @package console\controllers
 */
class WebsocketServerController extends Controller
{
  /**
   * @throws \Exception
   */
  public function actionStart()
  {
    /** @var Server $server */
    $server = Yii::$app->websocketServer;
    $server->start();
  }
}

... and run in the console:

#php yii websocket-server\start

For send data to server from backend as client:

    /** @var goodizer\websocket\Client $client */
    $client = Yii::$app->get('websocketClient');
    $client->send(json_encode(['method' => 'foo', 'params' => ['bar' => true]]));