承接 digitv/yii2sockets 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

digitv/yii2sockets

Composer 安装命令:

composer require digitv/yii2sockets

包简介

Node.js web-sockets support

README 文档

README

Node.js server integration module (web-sockets)

Install with composer

composer require digitv/yii2sockets:1.*

Add console command controller to the config (controllerMap section):

'controllerMap' => [
    ...
    'node-sockets' => '\digitv\yii2sockets\commands\YiiNodeSocketsController',
    ...
],

Add session component to the 'components' section of main config:

'components' => [
    ...
        'session' => [
            'class' => 'digitv\yii2sockets\YiiNodeSocketSession',
            'keyPrefix' => 'sess_preffix_',
            'timeout' => 600,
        ],
    ...
]

It uses Redis DB connection to share session data with Node.js server. Of course you must use redis component to connect Redis DB. Add it in both configs (web and main).

'components' => [
    ...
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ],
    ...
]

And add component do those configs (https not implemented yet):

'components' => [
    ...
        'nodeSockets' => [
            'class' => 'digitv\yii2sockets\YiiNodeSocket',
            'nodeJsHost' => 'localhost',
            'nodeJsPort' => 3001,
            'nodeJsScheme' => 'http',
            'nodeJsHostClient' => 'your-site.com',
            'serviceKey' => 'serviceKeyUsedForCommunication',
            'sessionKeyPrefix' => 'sess_preffix_',
            'channelsByPermissions' => [
                'channel_name' => 'permission',
                'channel_name_2' => [
                    'permission' => '@',
                    'url' => '/chat/index',     //URL on which users can connect to channel
                ],
            ],
        ],
    ...
]

Parameter channelsByPermissions is an array with channel names and permissions. If Yii::$app->user->can('permission') returns TRUE than channel channel_name will be added to user automatically. It runs on component init.

When configs are ready use console command to build Node.js config files:

./yii node-sockets/init

Node.js server

Server is in server subdirectory. Before first run you must install all Node.js modules. Go to server directory and install they with command

npm install

Now you can run server

node app.js

You can use environment variables to overwrite config options. For example:

NODE_ENV=production node app.js

In that case it runs in production mode with disabled debug information.

Messages

There are 3 message classes (I call it Frames):

  1. Basic (YiiNodeSocketFrameBasic) used for basic messages.

  2. jQuery (YiiNodeSocketFrameJQuery) used for jQuery DOM manipulations.

  3. Notify (YiiNodeSocketFrameGrowl) uses kartik-v/yii2-widget-growl to show messages for user.

  4. Alert (YiiNodeSocketFrameAlert) plays audio alert to user.

Examples

Send message with array[] body to channel test_channel. On front end will be used Javascript callback jsCallbackName.

Yii::$app->nodeSockets->newMessage()
    ->setBody([
        'id' => $model->id,
        'message' => Yii::t('app', 'You have new incoming call'),
    ])
    ->setChannel('test_channel')
    ->setCallback('jsCallbackName')
    ->send();

Send message to this socket (example is used on AJAX request).

Yii::$app->nodeSockets->newNotify()
    ->setText('Hello world')
    ->sendToThis();

Send jQuery frame to user with ID 1. It will remove element with selector #element_selector.

Yii::$app->nodeSockets->newJQuery()
    ->remove('#element_selector')
    ->setUser(1)
    ->send();

Send Alert frame to channel test_channel to all browser windows (not active too):

Yii::$app->nodeSockets->newAlert()
    ->setAudioId('audio_element_id')
    ->setChannel('test_channel')
    ->onlyActiveWindow(false)
    ->send();

Please notice, that you need to render block with <audio> element in order to play it.

Also includes example YiiNodeSocketFrameChat frame type that sends message depending on its content:

  1. to recipient_id and author_id if object contains both those properties;
  2. to channel composed automatically otherwise. You can see frame source for details.
$message = new ChatMessage([
    'recipient_id' => 4,
    'author_id' => 1,
    'message' => 'test text message',
]);

Yii::$app->nodeSockets->newChat()
    ->setMessage($message)
    ->send();

Javascript callback example:

YiiNodeSockets.callbacks.jsCallbackName = function (message, _socket) {
    console.log(message.body);
};

digitv/yii2sockets 适用场景与选型建议

digitv/yii2sockets 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 620 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nodejs」 「node」 「yii2」 「web-sockets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 digitv/yii2sockets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 digitv/yii2sockets 我们能提供哪些服务?
定制开发 / 二次开发

基于 digitv/yii2sockets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 620
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 4
  • Forks: 4
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-25