承接 obray/web-socket-server 相关项目开发

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

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

obray/web-socket-server

Composer 安装命令:

composer require obray/web-socket-server

包简介

PHP based WebSocket server.

README 文档

README

Small PHP WebSocket server library built on obray/socket-server.

This package handles the WebSocket upgrade, frame parsing, protocol close responses, and dispatches messages to an application handler. Application behavior such as chat rooms, alert routing, authentication, presence, and connection metadata should live in the handler that implements the library interface.

Install

composer require obray/web-socket-server

For local development with this checkout next to SocketServer, the example in examples/broadcast.php includes a small fallback autoloader.

Start An Echo Server

<?php

require __DIR__ . '/vendor/autoload.php';

$server = (new \obray\WebSocketServer())->create('127.0.0.1', 8080);
$server->start();

The default handler echoes text and binary messages, replies to ping frames with pong frames, and closes when the client sends a close frame. It is quiet by default. Pass a logger to see lifecycle messages:

$handler = new \obray\base\WebSocketBaseHandler(function ($message) {
    error_log($message);
});

$server = (new \obray\WebSocketServer())->create('127.0.0.1', 8080, null, $handler);
$server->start();

Custom Handler

Implement obray\interfaces\WebSocketServerHandlerInterface or extend obray\base\WebSocketBaseHandler and override only the callbacks you need.

class AlertsHandler extends \obray\base\WebSocketBaseHandler
{
    private $connections = [];

    public function onUpgraded($data, \obray\interfaces\SocketConnectionInterface $connection): void
    {
        $this->connections[spl_object_hash($connection)] = $connection;
    }

    public function onText(string $data, \obray\interfaces\SocketConnectionInterface $connection): void
    {
        foreach($this->connections as $client){
            if($client->isConnected()){
                \obray\WebSocket::sendText($client, $data);
            }
        }
    }

    public function onDisconnect(\obray\interfaces\SocketConnectionInterface $connection): void
    {
        unset($this->connections[spl_object_hash($connection)]);
    }
}

Sending Messages

Handlers can use the helper methods on obray\WebSocket instead of encoding frames directly:

\obray\WebSocket::sendText($connection, 'hello');
\obray\WebSocket::sendBinary($connection, $bytes);
\obray\WebSocket::sendPing($connection);
\obray\WebSocket::sendPong($connection);
\obray\WebSocket::sendClose($connection);
\obray\WebSocket::sendCloseWithCode($connection, 1000, 'done');

If your handler defines onStart(\obray\SocketServer $server), the WebSocket adapter will call it when the lower socket server starts. This is intentionally optional so application-specific concerns can stay outside the library.

public function onStart(\obray\SocketServer $server): void
{
    $server->watchTimer(5, 30, function () {
        // Application-owned recurring work, such as publishing alerts.
    });
}

Protocol Behavior

  • Client frames must be masked.
  • Reserved bits are rejected unless future extensions add support.
  • Invalid opcodes close only the offending connection.
  • Control frames cannot be fragmented and cannot exceed 125 bytes.
  • Frame and message payloads are limited to 1 MiB by default.
  • Protocol errors close with WebSocket close code 1002.
  • Oversized payloads close with WebSocket close code 1009.

Configure payload limits before creating or starting the server:

$webSocketServer = (new \obray\WebSocketServer())
    ->setMaximumFramePayloadLength(262144)
    ->setMaximumMessageLength(524288);

$server = $webSocketServer->create('127.0.0.1', 8080, null, $handler);
$server->start();

The smoke test exercises handshake, echo, ping/pong, partial reads, fragmented messages, clean close, unmasked frames, invalid opcodes, and oversized frames:

php tools/websocket_smoke.php

Production Notes

  • Run the process under a supervisor such as systemd, Supervisor, or another process manager.
  • Terminate TLS at a proxy or configure a secure StreamContext.
  • Keep authentication, authorization, rooms, routing, and persistence in your application handler.
  • Track application connection state in the handler, not in the library.
  • Use SocketServer::setLogger() for lower-level server logs if you need them.

obray/web-socket-server 适用场景与选型建议

obray/web-socket-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 155 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 obray/web-socket-server 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-05