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
Requires
- php: ^7.3
- workerman/workerman: ^4.0
- yiisoft/yii2: ^2.0
This package is not auto-updated.
Last update: 2024-11-20 17:00:35 UTC
README
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]]));