venndev/vosaka-wsock 问题修复 & 功能扩展

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

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

venndev/vosaka-wsock

最新稳定版本:1.0.0

Composer 安装命令:

composer require venndev/vosaka-wsock

包简介

A library for web-socket

README 文档

README

Vosaka Wsock is a high-performance, asynchronous WebSocket library for PHP, designed to be lightweight, extensible, and easy to use. It is built on top of Vosaka Fourotines, leveraging cooperative multitasking for efficient handling of concurrent connections.

Features

  • Asynchronous I/O: Fully non-blocking server implementation.
  • Room Support: Built-in support for rooms, allowing easy broadcasting to specific groups of clients.
  • Middleware: Extensible middleware system (e.g., for rate-limiting, authentication).
  • Handshake Control: Customizable handshake logic via onHandshake.
  • JSON Ready: Convenient methods for sending and receiving JSON messages.
  • Connection Management: Robust connection registry and tracking.

Installation

Install via Composer:

composer require venndev/vosaka-wsock

Quick Start

Create a simple chat server by extending AbstractWebSocketHandler:

use vosaka\wsock\connection\Connection;
use vosaka\wsock\connection\Message;
use vosaka\wsock\server\AbstractWebSocketHandler;
use vosaka\wsock\server\ServerConfig;
use vosaka\wsock\server\WebSocketServer;
use vosaka\wsock\connection\ConnectionRegistry;
use vosaka\foroutines\RunBlocking;
use function vosaka\foroutines\main;

final class ChatHandler extends AbstractWebSocketHandler {
    public function onOpen(Connection $connection): void {
        echo "New connection: {$connection->id}\n";
        $connection->sendText("Welcome to the server!");
    }

    public function onMessage(Connection $connection, Message $message): void {
        if ($message->isText()) {
            echo "Message from {$connection->id}: {$message->getContent()}\n";
            $connection->sendText("Echo: " . $message->getContent());
        }
    }
}

main(function () {
    RunBlocking::new(function () {
        $config = new ServerConfig(host: '0.0.0.0', port: 9000);
        $registry = new ConnectionRegistry();
        $server = new WebSocketServer(
            config: $config,
            handler: new ChatHandler($registry),
            registry: $registry
        );

        $server->start();
        echo "WebSocket server started on ws://localhost:9000\n";
    });
});

Core Components

  • WebSocketServer: The main server class that handles TCP connections and WebSocket handshakes.
  • AbstractWebSocketHandler: Interface/base class for defining your application logic (onOpen, onMessage, onClose).
  • Connection: Represents an active WebSocket client connection.
  • Room: A group of connections for easy broadcasting.
  • ConnectionRegistry: Manages all active connections and their room associations.

Extensions & Dependencies

License

This project is licensed under the MIT License - see the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固