scherersoftware/cake-websocket 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

scherersoftware/cake-websocket

Composer 安装命令:

composer require scherersoftware/cake-websocket

包简介

Websocket plugin for CakePHP

README 文档

README

CakePHP 3 Websocket  Plugin

License Latest Stable Version Latest Unstable Version Monthly Downloads

Introduction

This CakePHP 3 plugin gives you an easy way to add websocket capability to your web application.

Main Packages

Requirements

  • CakePHP 3.3 or higher
  • PHP 7.1

Usage in 4 easy steps

Note: You can checkout our CakePHP App Template for testing it on a clean app setup with preinstalled dependencies.

1. Define a new event

Example for websocket_events.php

...
'userDataUpdated' => [
    'audience' => [
        'includeAllNotAuthenticated' => false,
        'includeAllAuthenticated' => true
    ]
]
...

2. Publish the event in server context (e.g. Shell, Controller, Table...)

Example for UsersController.php

...
use Websocket\Lib\Websocket;
...
if ($this->Users->save($exampleUser)) {
    Websocket::publishEvent('userDataUpdated', ['editedUserId' => $exampleUser->id]);
}
...

3. Let the client receive the event and define a callback

Example for ../users/index_controller.js

...
App.Websocket.onEvent('userDataUpdated', function(payload) {
    if (payload.editedUserId === this.exampleUser.id) {
        alert('Someone changed the data of this user!');
    }
}.bind(this));
...

4. Run the websocket server shell and start testing!

$ bin/cake websocket_server

Installation

1. Require the plugin

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require scherersoftware/cake-websocket

2. Load the plugin

The next step is to load the plugin properly inside your bootstrap.php:

Plugin::load('Websocket', ['bootstrap' => true, 'routes' => true]);

3. Configure app config

  • File: /config/app.php

    <?php
    ...
    'Websocket' => [
        'ssl' => false,
        'host' => '127.0.0.1',
        'externalHost' => 'cws.dev',
        'port' => 8889,
        'frontendPath' => [
            'ssl' => [
                'path' => '/wss/',
                'usePort' => false
            ],
            'normal' => [
                'path' => '/',
                'usePort' => true
            ]
        ],
        'sessionCookieName' => 'cws',
        'Queue' => [
            'name' => 'websocket',
            'loopInterval' => 0.1,
        ]
    ]
    ...
    

4. Create and configure websocket events

  • File: /config/websocket_events.php

    <?php
    return [
        'userDataUpdated' => [
            'audience' => [
                'includeAllNotAuthenticated' => false,
                'includeAllAuthenticated' => true
            ]
        ]
    ];
    

5. Configure AppController.php

In your src/Controller/AppController.php, insert the following pieces of code

Usage:

use Websocket\Lib\Websocket;

beforeFilter():

...
$this->FrontendBridge->setJson('websocketFrontendConfig', Websocket::getFrontendConfig());
...

6. Make the JS websocket lib globally accessible under App.Websocket

  • Load the file /webroot/lib/websocket.js after loading the Frontend Bridge assets

7. Setup sessions properly if not alread done

Please follow the Cake Sessions Documentation

8. Setup Apache SSL ProxyPass if necessary

Make sure these modules are activated:

  • mod_proxy.so
  • mod_proxy_wstunnel.so

Edit your vhosts configuration and add this to the ssl section:

ProxyPass /wss/ ws://localhost:8889/

Roadmap

1.1.0

  • Unit Tests
  • Add a websocket_connections table which stores all active connections

scherersoftware/cake-websocket 适用场景与选型建议

scherersoftware/cake-websocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.93k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2017 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 scherersoftware/cake-websocket 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 19.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 3
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-25