定制 sentixtech/websocket 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sentixtech/websocket

最新稳定版本:1.0.1

Composer 安装命令:

composer require sentixtech/websocket

包简介

Advanced WebSocket package for Laravel with broadcasting capabilities

README 文档

README

Overview

A powerful, flexible WebSocket implementation for Laravel applications, enabling real-time communication across various use cases.

Features

  • Dynamic channel creation
  • Flexible subscription management
  • Generic event broadcasting
  • User-specific and global messaging
  • Comprehensive error handling
  • Detailed logging

Installation

Composer Installation

composer require sentixtech/websocket

Publish Configuration

php artisan vendor:publish --provider="SentixTech\WebSocket\WebSocketServiceProvider"

Configure WebSocket Server

Edit config/websocket.php:

return [
    'host' => env('WEBSOCKET_HOST', 'localhost'),
    'port' => env('WEBSOCKET_PORT', 8080),
    'max_clients' => env('WEBSOCKET_MAX_CLIENTS', 1000),
    'max_frame_size' => env('WEBSOCKET_MAX_FRAME_SIZE', 1024 * 1024), // 1MB
];

Start WebSocket Server

php artisan websocket:serve

Usage Examples

Channel Management

// Create channels
WebSocket::createChannel('notifications');
WebSocket::createChannel('user_events');

Subscription

// Subscribe a client to a channel
WebSocket::subscribe($socketResource, 'notifications', $userId);

// Unsubscribe from a channel
WebSocket::unsubscribe($socketResource, 'notifications', $userId);

Broadcasting

// Broadcast to all channel subscribers
WebSocket::broadcast('notifications', [
    'type' => 'alert',
    'message' => 'System maintenance in 10 minutes'
]);

// Broadcast to specific users
WebSocket::broadcast('notifications', $message, [
    'users' => [1, 2, 3] // Only send to these user IDs
]);

Event Emission

// Emit a generic event
WebSocket::emit('user_login', [
    'user_id' => 123,
    'timestamp' => now()
]);

Channel Monitoring

// Get number of channel subscribers
$subscriberCount = WebSocket::subscribers('notifications');

Advanced Usage

Real-time Notifications

// Send a notification to specific users
WebSocket::broadcast('notifications', [
    'title' => 'New Message',
    'content' => 'You have a new message from John',
    'user_ids' => [5, 10] // Only notify these users
]);

Chat System Integration

// Send a chat message
WebSocket::broadcast('chat_room_1', [
    'sender_id' => Auth::id(),
    'message' => $messageContent
]);

Security

  • Supports user-specific channel subscriptions
  • Flexible access control
  • Comprehensive error logging

Performance

  • Non-blocking socket implementation
  • Configurable max clients and frame size
  • Efficient channel and user management

Error Handling

All methods return boolean or integer status:

  • createChannel(): Returns true/false
  • subscribe(): Returns true/false
  • broadcast(): Returns number of successful broadcasts
  • emit(): Returns number of successful event emissions

Troubleshooting

  • Check storage/logs/laravel.log for WebSocket errors
  • Ensure WebSocket server is running
  • Verify configuration in config/websocket.php

Requirements

  • PHP 7.4+
  • Laravel 8.0+
  • Sockets extension

Contributing

Contributions are welcome! Please submit pull requests to the repository.

License

MIT License

Support

For issues and support, please open a GitHub issue in the repository.

统计信息

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

GitHub 信息

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

其他信息

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