承接 cydrickn/swoole-websocket-bundle 相关项目开发

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

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

cydrickn/swoole-websocket-bundle

Composer 安装命令:

composer require cydrickn/swoole-websocket-bundle

包简介

Swoole Websocket for Symfony

README 文档

README

A bundle for websocket using swoole

Requirements

Intallation

Add the package

composer require cydrickn/swoole-websocket-bundle

Run the server

php ./bin/console websocket:server

Commands

websocket:server

Run the websocket server

Options Details Default
host The host of the server 127.0.0.1
port The port of the server 8000

Events

This bundle will have an event where you can listen

\Cydrickn\SwooleWebsocketBundle\Event\OpenEvent

This event will be trigger once a client have been connected to the server.

\Cydrickn\SwooleWebsocketBundle\Event\MessageEvent

This event will be trigger once the client send a message

\Cydrickn\SwooleWebsocketBundle\Event\CloseEvent

This event will be trigger once a client have been disconnected

Client connection using Javascript

For the basic way on connecting to websocket you can use the Browser Websocket

const socket = new WebSocket('ws://localhost:8080');

// Connection opened
socket.addEventListener('open', (event) => {
    socket.send('Hello Server!');
});

// Listen for messages
socket.addEventListener('message', (event) => {
    console.log('Message from server ', event.data);
});

Use it in runtime

Using the command is fine, but if you happen you want to serve this using symfony runtime you can do so.

  1. First you need to include symfony/runtime
    composer require symfony/runtime
  2. Next run your public/index.php with env APP_RUNTIME
    APP_RUNTIME=\\Cydrickn\\SwooleWebsocketBundle\\Runtime\\Runtime php ./public/index.php

Runtime Configuration

For runtime configuration you can add it to your public/index.php

Key Details Default
host The host of the server 127.0.0.1
port The port of the server 8000
mode The mode for server 2 / SWOOLE_PROCESS
sock_type The socket type for server 1 / SWOOLE_SOCK_TCP
settings The setting is base on swoole configuration [] / Empty Array
serve_http Include to serve HTTP false

Example:

#!/usr/bin/env php
<?php

use App\Kernel;

$_SERVER['APP_RUNTIME_OPTIONS'] = [
    'host' => '0.0.0.0',
    'port' => 8000,
    'mode' => SWOOLE_PROCESS,
    'settings' => [
        \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2,
        \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true,
        \Swoole\Constant::OPTION_DOCUMENT_ROOT => dirname(__DIR__).'/public'
    ],
];

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

Also instead of having to server for websocket and http, you can just enable the Serve HTTP, by setting the serve_http to true

Hot reloading

We already include a hor reloading for this bundle. To enable it you need to first require cydrickn/php-watcher

Hot reloading is only available if you are using runtime

composer require cydrickn/php-watcher

Now add to your APP_RUNTIME_OPTIONS the hotReload The basePath should be your project folder

$_SERVER['APP_RUNTIME_OPTIONS'] = [
    'host' => '0.0.0.0',
    'port' => 8000,
    'mode' => SWOOLE_PROCESS,
    'hotReload'=> [
        'enabled' => true,
        'basePath' => dirname(__DIR__),
    ],
    'settings' => [
        \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2,
        \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true,
    ],
];

Socket IO

In this bundle you can also enable using socket.io implementation, to enable it first you need to install cydrickn/socketio

This implementation is only available if you are using runtime

composer require cydrickn/socketio

Now add to your APP_RUNTIME_OPTIONS set the socketio to true

$_SERVER['APP_RUNTIME_OPTIONS'] = [
    'socketio' => true,
    'host' => '0.0.0.0',
    'port' => 8000,
    'mode' => SWOOLE_PROCESS,
    'settings' => [
        \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2,
        \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true,
    ],
];

Adding a route to socket io

For pure socket io you will do this

$server->on('connection', function (\Cydrickn\SocketIO\Socket $socket) {
    $socket->emit('hello', 'world');
});

$server->on('chat', function (\Cydrickn\SocketIO\Socket $socket, $message) {
    $socket->broadcast()->emit('chat', $message);
});

You will use the Attribute Route of this bundle Cydrickn\SwooleWebsocketBundle\Attribute\RouteAttribute In any of your service

<?php
namespace App\Controller;

use Cydrickn\SwooleWebsocketBundle\Attribute\RouteAttribute;

class SocketController
{
   #[RouteAttribute(path: 'connection')]
   public function connection(\Cydrickn\SocketIO\Socket $socket)
   {
      $socket->emit('hello', 'world');
   }
   
   #[RouteAttribute(path: 'chat')]
   public function anotherMethod(\Cydrickn\SocketIO\Socket $socket, $message)
   {
      $socket->broadcast()->emit('chat', $message);
   }
}

Related Package

cydrickn/swoole-websocket-bundle 适用场景与选型建议

cydrickn/swoole-websocket-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 4, 最近一次更新时间为 2022 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-24