承接 wheakerd/sse-client 相关项目开发

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

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

wheakerd/sse-client

最新稳定版本:v1.0.0

Composer 安装命令:

composer require wheakerd/sse-client

包简介

SSE coroutine client based on swoole implementation, suitable for hyperf or super-kernel.

README 文档

README

Wheakerd 提供了对 Server Sent Event Client 的封装,可基于 wheakerd/sse-client 组件对 SSE (server sent event) 进行访问。

安装

composer require wheakerd/sse-client

使用

组件提供了一个 Wheakerd\SseClient\Client 来创建客户端,我们直接通过代码来演示一下:

<?php
declare(strict_types=1);

namespace App\Controller;

use Wheakerd\SseClient\Client;


final class IndexController
{
    public function index()
    {
        $client = new Client('https://www.example.com', 0.5);
        
        if (!$client->isConnected()) {
            return '无法连接到服务器';
        }
        
        //  如果第三方要求鉴权
        $sseClient->setHeader('Authorization', 'Bearer your token');
        
        $res = $sseClient->send(
			[
				'data' => '...',
			],
		);
		
		//  如果发送失败
		if (false === $send) {
		    //  获取错误原因
			return $client->sta();
		}
		
		while (true) {
			$chunk = $client->recv();
			
			//  全等于空 直接关闭连接
			if ('' === $chunk) {
			    $client->close();
			}
			
			$chunkData = $client->decode($chunk);

			if (null === $chunkData) {
				continue;
			}
			if (false === $chunkData) {
				break;
			}

			$eventStreamResponse->success('message', $chunk);
		}

		$eventStreamResponse->close();
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-03