sockeon/laravel 问题修复 & 功能扩展

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

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

sockeon/laravel

Composer 安装命令:

composer require sockeon/laravel

包简介

Laravel integration for Sockeon WebSocket and HTTP server

README 文档

README

Laravel integration for Sockeon — a real-time WebSocket and HTTP server for PHP.

This package wires Sockeon into Laravel's service container, config system, logging, and Artisan commands so you can run a Sockeon server alongside your Laravel app without a standalone server.php bootstrap.

Requirements

  • PHP 8.3+
  • Laravel 11, 12, or 13
  • sockeon/sockeon ^3.0
  • ext-sockets, ext-openssl (required by Sockeon)
  • ext-openswoole (optional, for engine=swoole)
  • ext-redis (optional, for multi-node scaling)

Installation

composer require sockeon/laravel

Run the installer to publish config and scaffold directories:

php artisan sockeon:install

Or publish config only:

php artisan vendor:publish --tag=sockeon-config

Quick start

  1. Create a controller in app/Sockeon/Controllers/:
<?php

namespace App\Sockeon\Controllers;

use Sockeon\Sockeon\Controllers\SocketController;
use Sockeon\Sockeon\Http\Attributes\HttpRoute;
use Sockeon\Sockeon\Http\Request;
use Sockeon\Sockeon\Http\Response;
use Sockeon\Sockeon\WebSocket\Attributes\OnConnect;
use Sockeon\Sockeon\WebSocket\Attributes\SocketOn;

class ChatController extends SocketController
{
    #[OnConnect]
    public function onConnect(string $clientId): void
    {
        $this->emit($clientId, 'welcome', ['clientId' => $clientId]);
    }

    #[SocketOn('chat.message')]
    public function onMessage(string $clientId, array $data): void
    {
        $this->broadcast('chat.message', [
            'clientId' => $clientId,
            'message' => $data['message'] ?? '',
        ]);
    }

    #[HttpRoute('GET', '/api/status')]
    public function status(Request $request): Response
    {
        return Response::json(['clients' => $this->getClientCount()]);
    }
}
  1. Start the server:
php artisan sockeon:serve

Controllers in app/Sockeon/Controllers are auto-discovered by default. You can also register classes manually in config/sockeon.php under controllers.

Configuration

All options live in config/sockeon.php. Key environment variables:

Variable Default Description
SOCKEON_HOST 0.0.0.0 Bind address
SOCKEON_PORT 6001 Listen port
SOCKEON_DEBUG false Verbose logging (sets log level when SOCKEON_LOG_LEVEL is unset)
SOCKEON_LOG_LEVEL null Minimum log level (debug, info, warning, etc.)
SOCKEON_LOG_CONSOLE true Write logs to stdout
SOCKEON_LOG_FILE true Write logs to disk
SOCKEON_LOG_DIRECTORY storage/logs/sockeon Log file directory
SOCKEON_LOG_SEPARATE_FILES false Split logs by level into subdirectories
SOCKEON_ENGINE stream_select stream_select or swoole
SOCKEON_AUTO_DISCOVER true Scan app/Sockeon/Controllers
SOCKEON_RATE_LIMIT_ENABLED false Enable HTTP/WebSocket rate limits
SOCKEON_TRUST_PROXY false Trust reverse-proxy headers
SOCKEON_MIDDLEWARE_AUTO_DISCOVER false Scan app/Sockeon/Middleware

See the Sockeon docs for survivability, scaling, CORS, and rate limiting options.

Middleware

Register global middleware in config/sockeon.php:

'middleware' => [
    'http' => [
        App\Sockeon\Middleware\AuthMiddleware::class,
    ],
    'websocket' => [
        App\Sockeon\Middleware\AuthenticateSocket::class,
    ],
    'handshake' => [
        App\Sockeon\Middleware\VerifyAuthKey::class,
    ],
],

Per-route middleware uses HttpRoute / SocketOn attributes on your controllers. See Middleware.

Logging

Sockeon uses its own logger (colored console output and optional file logging). Configure it in config/sockeon.php or via env vars:

SOCKEON_LOG_LEVEL=info
SOCKEON_LOG_FILE=true
SOCKEON_LOG_DIRECTORY=/var/log/sockeon

Artisan commands

Command Description
sockeon:install Publish config and create app/Sockeon directories
sockeon:serve Start the WebSocket/HTTP server

Facade

use Sockeon\Laravel\Facades\Sockeon;

$server = Sockeon::server();
$server->broadcast('notification', ['message' => 'Hello']);

The server instance is created once per process and controllers are resolved through Laravel's container (constructor injection works).

Testing

composer test

Documentation

Security

If you discover a security vulnerability, please email xentixar@gmail.com.

License

MIT — see LICENSE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固