hosmelq/sse-saloon 问题修复 & 功能扩展

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

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

hosmelq/sse-saloon

最新稳定版本:v0.1.0

Composer 安装命令:

composer require hosmelq/sse-saloon

包简介

A Saloon plugin for consuming Server-Sent Events (SSE) streams.

README 文档

README

A Saloon plugin that adds Server-Sent Events support to your HTTP requests. Built on the hosmelq/sse-php library for WHATWG-compliant SSE parsing.

Requirements

  • PHP 8.2+

Installation

composer require hosmelq/sse-saloon

Basic Usage

Add the HasServerSentEvents trait to your request class:

use HosmelQ\SSE\Saloon\Traits\HasServerSentEvents; use Saloon\Http\Request; use Saloon\Enums\Method; class StreamNotifications extends Request { use HasServerSentEvents; protected Method $method = Method::GET; public function resolveEndpoint(): string { return '/notifications/stream'; } }

Send the request and process events:

$response = $connector->send(new StreamNotifications()); foreach ($response->asEventSource()->events() as $event) { echo "Data: {$event->data}\n"; echo "Event: {$event->event}\n"; echo "ID: {$event->id}\n"; }

Custom Headers and Configuration

Override the default methods to add authentication, custom headers, or configure connection settings:

use HosmelQ\SSE\Saloon\Traits\HasServerSentEvents; use Saloon\Http\Request; use Saloon\Enums\Method; class CustomizedStream extends Request { use HasServerSentEvents { defaultConfig as defaultSSEConfig; defaultHeaders as defaultSSEHeaders; } protected Method $method = Method::GET; public function __construct(private int $readTimeout, private string $token) {} public function resolveEndpoint(): string { return '/api/stream'; } protected function defaultConfig(): array { return array_merge($this->defaultSSEConfig(), [ 'read_timeout' => $this->readTimeout, ]); } protected function defaultHeaders(): array { return array_merge($this->defaultSSEHeaders(), [ 'Authorization' => 'Bearer ' . $this->token, ]); } }

For detailed information about event processing, parsing, and handling, see the hosmelq/sse-php documentation.

Error Handling

use HosmelQ\SSE\SSEProtocolException; try { $response = $connector->send(new EventStreamRequest()); foreach ($response->asEventSource()->events() as $event) { } } catch (SSEProtocolException $e) { echo 'SSE Error: ' . $e->getMessage(); }

Testing

composer test

Changelog

See CHANGELOG.md for a list of changes.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固