承接 dimogrudev/php-websocket 相关项目开发

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

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

dimogrudev/php-websocket

Composer 安装命令:

composer require dimogrudev/php-websocket

包简介

No-dependency implementation of WebSocket server in PHP

README 文档

README

A zero-dependency native implementation of WebSocket server in PHP.

Lightweight and minimalistic.

Features

  • Strict adherence to RFC 6455
  • Automatic HTTP Upgrade handshake compliant with RFC 9110 and RFC 9112
  • SSL/TLS encryption support
  • Binary and textual data message support, both sending and receiving
  • Transparent reassembly of incoming fragmented messages
  • Built-in handling for Ping, Pong, and Close control frames
  • User-defined non-blocking timers
  • Non-blocking I/O

Requirements

  • PHP 8.4 or higher (64-bit)

Important

If you plan to run websockets on a shared hosting, note that most providers block ports for any third-party usage. A VPS or dedicated server is highly recommended.

Installation

This library may be installed via Composer:

composer require dimogrudev/php-websocket

Usage

use WebSocket\Server;
use WebSocket\Contract\ClientInterface;
use WebSocket\Contract\RequestInterface;
use WebSocket\Contract\MessageInterface;

require 'vendor/autoload.php';

// Create an instance of the server class
// 0.0.0.0 is set as host to make the server reachable at all IPv4 addresses
$server = new Server('0.0.0.0', 8443);
// Enable encryption and provide certificate files
$server->encryption(true, 'path/to/cert.crt', 'path/to/cert.key');

// Handle incoming messages and respond to clients
$server->onMessageReceive(function (ClientInterface $client, MessageInterface $message): void {
    if ($message->isBinary) {
        echo "{$client->ipAddr} (#{$client->id}) sends binary message ({$message->length} bytes)\n";

        // Echo the binary message back to the client
        $client->send($message->payload, isBinary: true);
    } else {
        echo "{$client->ipAddr} (#{$client->id}) sends `{$message->payload}`\n";

        // Reply with a text message
        $client->send("Server received your message: {$message->payload}");
    }
});

$server->start();

Tip

Modern browsers block non-secure WebSocket connections (ws://) on secure websites (https://). Always use a valid SSL/TLS certificate (e.g. Let's Encrypt) for production.

Timers

// Create a timer to run function repeatedly with a 500 milliseconds interval
// It provides timer ID which may be used for later cancellation
$timerId = $server->setTimer(function (): void {
    // Your logic here
}, 500, true);

// Cancel the timer
$server->clearTimer($timerId);

Callbacks Reference

Method Signature Description
onServerStart (): void Triggered when the server starts listening.
onServerStop (): void Triggered when the server stops.
onHandshake (ClientInterface, RequestInterface): bool Triggered when a handshake request is received. Return true to accept the connection.
onClientConnect (ClientInterface): void Triggered after the handshake is accepted and the connection is fully established.
onClientDisconnect (ClientInterface): void Triggered when the connection is closed.
onMessageReceive (ClientInterface, MessageInterface): void Triggered when a complete data message is received.

Connection Security

Unauthorized connections may be rejected by using the onHandshake callback:

$server->onHandshake(function (ClientInterface $client, RequestInterface $request): bool {
    // Reject connection if it's not from your trusted domain
    if ($request->header('origin') !== 'https://example.com') {
        return false;
    }
    return true;
});

License

The MIT License (MIT). For more information, please see LICENSE.

dimogrudev/php-websocket 适用场景与选型建议

dimogrudev/php-websocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「websocket」 「server」 「rfc6455」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-19