paragi/php-websocket-client 问题修复 & 功能扩展

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

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

paragi/php-websocket-client

Composer 安装命令:

composer require paragi/php-websocket-client

包简介

Websocket client

README 文档

README

Use PHP to connect to at websocket service. These 3 methods make the websocket negotiation and connection and handle the hybi10 frame encoding required.

Note : unlike the library ratchetphp/Pawl, this a synchronous library that could be used within blocking kernels like Symfony for example.

Example 1:

try {
   $sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',80) );
   $sp->write("hello server");
   echo "Server responded with: " . $sp->read();
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
   echo "Something gets wrong ".$e->getMessage();
}

Example 2, using a session cookie and setting timeout:

try {
    $headers = ["Cookie: SID=".session_id()];
    $sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',80,$headers,$errstr,16);
    $bytes_written = $sp->write("hello server");
    $data = $sp->read();
    echo "Server responded with: ". $data;
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
   echo "Something gets wrong ".$e->getMessage();
}

Example 3, using SSL

try {
    $sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',443,'',$errstr, 10,true) ) {
    $sp->write("hello server");
    echo "Server responded with: " . $sp->read();
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
   echo "Something gets wrong ".$e->getMessage();
}

Client class

Constructor

Open websocket connection

__construct(string $host [, int $port [, array $additional_headers [, string &error_string [, int $timeout [, resource $context]]]]] )

host A host URL. It can be a domain name like www.example.com or an IP address like local host: 127.0.0.1

port The servers port number

headers (optional) additional HTTP headers to attach to the request. For example to parse a session cookie.

error_string (optional) A referenced variable to store error messages, if any.

timeout (optional) The maximum time in seconds, a read operation will wait for an answer from the server. Default value is 10 seconds.

context (optional) A stream context resource created with stream_context_create() used to set various socket stream options.

write

Send data to server through the websocket, using hybi10 frame encoding.

int write(string $data [,boolean $final])

data Data to transport to server

final (optional) indicate if this block is the final data block of this request. Default true

read

Read data through websocket from the server, using hybi10 frame encoding.

string read([string &error_string])

error_string (optional) A referenced variable to store error messages, i any.

Note:

  • This implementation waits for the final chunk of data, before returning.
  • Reading data while handling/ignoring other kind of packages

Exception

If anything goes wrong (connection, write, read...), a ConnectionException is thrown back. Please catch them all to return a comprehensive message (here's is a sample in a Symfony context) :

try {
    $sp = new \Paragi\PhpWebsocket\Client($this->localIp, $this->wsPort, ['X-Pusher: Symfony']);
    $sp->write($data);
    $reading = $sp->read();
    $ret = "Server responded with: $reading";

    return new JsonResponse(['level' => 'success', 'message' => $ret], Response::HTTP_OK);
} catch (ConnectionException $ex) {
    return new JsonResponse(['level' => 'error', 'message' => $ex->getMessage()], Response::HTTP_SERVICE_UNAVAILABLE);
}

Tests

Tests are running against a local echo server implemented with Ratchet. You'll have to manually start the server in ANOTHER process.

$ php tests/bin/echoserver.php

And now you can launch PhpUnit

$ vendor/bin/phpunit 

Code coverage

With PhpDbg, just launch :

$ phpdbg -qrr vendor/bin/phpunit
$ firefox .coverage/index.html

Contribute

Please let me know if there is any problems with the code. Any contributions are accepted, if the code looks nice, not bloated and otherwise reasonable.

License: MIT: Free

TODO

  • Achieving 100% Code Coverage

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

paragi/php-websocket-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.18k 次下载、GitHub Stars 达 143, 最近一次更新时间为 2022 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 143
  • Watchers: 8
  • Forks: 59
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-22