定制 hosmelq/sse-saloon 二次开发

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

邮箱: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.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-15