immusen/yii2-swoole-websocket
Composer 安装命令:
composer require immusen/yii2-swoole-websocket
包简介
Websocket server for Yii2 base on swoole 4, Support JSONRPC, Resolve 'method' as a route reflect into controller/action, And support http or redis pub/sub to trigger async task from your web application.
README 文档
README
Websocket server for Yii2 base on swoole 4, Support JSON-RPC, Resolve 'method' as a route reflect into controller/action, And support http or redis pub/sub to trigger async task from your web application.
Installation
Install Yii2: Yii2.
Install swoole: swoole, recommend version 4+.
Other dependency: php-redis extension.
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist immusen/yii2-swoole-websocket "~1.0"
or add
"immusen/yii2-swoole-websocket": "~1.0"
to the require section of your composer.json file.
Test or Usage
after installation, cd project root path, e.g. cd yii2-advanced-project/
mv vendor/immusen/yii2-swoole-websocket/example/websocket ./
mv vendor/immusen/yii2-swoole-websocket/example/websocket-server ./
chmod a+x ./websocket-server
run:
./websocket-server start //start server
./websocket-server restart //restart server
./websocket-server reload //reload task worker
./websocket-server status //server status
config :
vim ./websocket/config/params.php
or coding in ./websocket/controllers/
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!"
}
}
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 JSON-RPC 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"
}
}
}
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']);
immusen/yii2-swoole-websocket 适用场景与选型建议
immusen/yii2-swoole-websocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.85k 次下载、GitHub Stars 达 33, 最近一次更新时间为 2019 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「websocket」 「extension」 「async」 「yii2」 「swoole」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 immusen/yii2-swoole-websocket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 immusen/yii2-swoole-websocket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 immusen/yii2-swoole-websocket 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Microservice RPC through message queues.
A custom URL rule class for Yii 2 which allows to create translated URL rules
The CodeIgniter Redis package
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
贝嘟分布式缓存扩展
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 8.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 33
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2019-02-26