定制 phrity/websocket 二次开发

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

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

phrity/websocket

最新稳定版本:3.6.2

Composer 安装命令:

composer require phrity/websocket

包简介

WebSocket client and server

README 文档

README

Phrity Websocket

Websocket Client and Server for PHP

Build Status

This library contains WebSocket client and server for PHP.

The client and server provides methods for reading and writing to WebSocket streams.

This repo replaces the abandoned textalk/websocket repo and is maintained by Sören Jensen, who has been maintaining the original since v1.3.

Some features

  • Client and multi-connection Server
  • ws (TCP) and wss (SSL) support
  • Listener callbacks on incoming messages and other events
  • Close and Ping/Pong handling (standard middlewares)
  • Deflate compression (middleware)
  • Additional optional middlewares and ability to create own middlewares
  • Support message fragmentation and payload masking

Documentation

Migration

  • v1 -> v2 - How to migrate from v1 to v2
  • v2 -> v3 - How to migrate from v2 to v3

Installing

Preferred way to install is with Composer.

composer require phrity/websocket

Client

The client can read and write on a WebSocket stream. It internally supports Upgrade handshake and implicit close and ping/pong operations.

Set up a WebSocket Client for request/response strategy.

$client = new WebSocket\Client("wss://echo.websocket.org/");
$client
    // Add standard middlewares
    ->addMiddleware(new WebSocket\Middleware\CloseHandler())
    ->addMiddleware(new WebSocket\Middleware\PingResponder())
    ;

// Send a message
$client->text("Hello WebSocket.org!");

// Read response (this is blocking)
$message = $client->receive();
echo "Got message: {$message->getContent()} \n";

// Close connection
$client->close();

Set up a WebSocket Client for continuous subscription

$client = new WebSocket\Client("wss://echo.websocket.org/");
$client
    // Add standard middlewares
    ->addMiddleware(new WebSocket\Middleware\CloseHandler())
    ->addMiddleware(new WebSocket\Middleware\PingResponder())
    // Listen to incoming Text messages
    ->onText(function (WebSocket\Client $client, WebSocket\Connection $connection, WebSocket\Message\Message $message) {
        // Act on incoming message
        echo "Got message: {$message->getContent()} \n";
        // Possibly respond to server
        $client->text("I got your your message");
    })
    ->start();

Server

The server is a multi connection, listening server. It internally supports Upgrade handshake and implicit close and ping/pong operations.

Set up a WebSocket Server for continuous listening

$server = new WebSocket\Server();
$server
    // Add standard middlewares
    ->addMiddleware(new WebSocket\Middleware\CloseHandler())
    ->addMiddleware(new WebSocket\Middleware\PingResponder())
    // Listen to incoming Text messages
    ->onText(function (WebSocket\Server $server, WebSocket\Connection $connection, WebSocket\Message\Message $message) {
        // Act on incoming message
        echo "Got message: {$message->getContent()} \n";
        // Possibly respond to client
        $connection->text("I got your your message");
    })
    ->start();

License

ISC License

统计信息

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

GitHub 信息

  • Stars: 216
  • Watchers: 5
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: ISC
  • 更新时间: 2026-01-04