ghostwalker/laravelwebsocket 问题修复 & 功能扩展

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

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

ghostwalker/laravelwebsocket

Composer 安装命令:

composer require ghostwalker/laravelwebsocket

包简介

websocket for laravel, based on Ratchet

README 文档

README

You can install the package via composer:

composer require ghostwalker/laravelwebsocket

Usage

register service-provider

add line to /config/app.php in array "providers"

Ghostwalker\LaravelWebsocketServiceProvider::class

.

then publish config

php artisan vendor:publish --provider="Ghostwalker\LaravelWebsocketServiceProvider" --tag="config"

.

/config/laravelwebsocket.php

example:

<?php

return [
    'httphost' => 'localhost',
    'port' => 8081,
    'sockets_dir' => 'var/www/html/sockets',
    'artisan_command_start' => 'websocket:start',
];

.

Now in this folder you need to create a class that will implement from the MessageComponentInterface and AddRouteContract interfaces

example:

<?php

namespace Sockets;

use Ghostwalker\Contracts\AddRouteContract;
use Ghostwalker\LaravelWebsocket;
use JetBrains\PhpStorm\Pure;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;


class mainSocket implements MessageComponentInterface, AddRouteContract
{
    protected \SplObjectStorage $clients;

    #[Pure] public function __construct()
    {
        $this->clients = new \SplObjectStorage();
    }

    public function onOpen(ConnectionInterface $conn)
    {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
        foreach ($this->clients as $client) {
            if ($from != $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn)
    {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e)
    {
        $conn->close();
    }

    public static function addRout(): void
    {
        LaravelWebsocket::$app->route('/chat', new self(), array('*'));
    }
}

in the addRout method, you need to change the route of your socket

.

it remains only to start our socket server with the php artisan websocket command (command to run as a daemon, it will run until the terminal closes. I recommend using screen)

Modules

you can create an infinite number of classes and all of them will be processed, which allows the system to be flexible

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固