eislambey / eventsource
简单的 EventSource / SSE (服务器端事件) 客户端。
v0.1.0
2019-08-07 16:41 UTC
Requires
- ext-curl: *
- ext-json: *
This package is auto-updated.
Last update: 2024-09-08 05:08:47 UTC
README
PHP 的简单 EventSource / SSE (服务器端事件) 客户端。
安装
$ composer require eislambey/eventsource
使用
示例:连接到端点并读取 5 条消息然后关闭。
<?php require_once dir(__FILE__) . '/vendor/autoload.php'; use EventSource\Event; use EventSource\EventSource; $es = new EventSource("http://example.com"); $messageReceived = 0; $es->onMessage(function (Event $event) use(&$messageReceived, $es) { if($es === 4){ $es->abort(); } $messageReceived++; echo $event->data, "\n"; }); $es->connect();
方法
__construct(string $url)
使用给定的 URL 创建新实例。
onMessage(callable $fn): void
设置当接收到消息时调用的函数。
connect(): void
连接到端点并接收消息。
abort(): void
终止连接并停止接收消息。
setCurlOptions(array $options): void
您可以设置任何 cURL 选项(如 cookies 和 headers),但不包括 CURLOPT_WRITEFUNCTION
、CURLOPT_NOPROGRESS
、CURLOPT_PROGRESSFUNCTION
。
参见:https://php.ac.cn/manual/tr/function.curl-setopt.php
授权协议
MIT 许可协议 (MIT)。请参阅 LICENSE 了解更多信息。