htrnf/sse-server
Composer 安装命令:
composer require htrnf/sse-server
包简介
A small PHP library for webRTC via Server Sent Events. A nice alternative to Websockets for shared hosting.
README 文档
README
A small PHP library for webRTC via Server Sent Events. A nice alternative to Websockets for shared hosting. Build your own controller via a single simple interface & its ready to go.
Installation
$ composer require htrnf/sse-server
Quick example
PHP
events.php:
<?php require __DIR__."/vendor/autoload.php"; use SSEServer\SSEServer; use SSEServer\SSEControllerInterface; class ExampleController implements SSEControllerInterface{ private $events; function __construct(){ //We will store our Event objects into "Events" $this->events = new Events; } /** * Each cycle while running, this function will be called. * This is where you should implement main logic for detecting new events * and populate $this->events with them using $this->events->push(). */ public function cycle(Int $currentCycle): Void{ //We will randomly send a message $random = rand(0, 10); switch($random){ case 0: //Push the new event into the Events object $this->events->push(new Event("message", Array("text" => "Hello"))); break; case 1: $this->events->push(new Event("message", Array("text" => "Howdy?"))); break; case 2: $this->events->push(new Event("message", Array("text" => "Do you like banana pancakes?"))); break; case 3: $this->events->merge( new Events( Array( new Event("message", Array("text" => "Parrots are the coolest animals!")), new Event("message", Array("text" => "I want to get one as a pet, do you know where i could get one?")) ) ) ); break; case 4: $this->events->push(new Event("message", Array("text" => "Please go to the grocery store and get some bread."))); break; case 5: $this->events->push(new Event("alert", Array("text" => "You have a scheduled meeting in 5 minutes, you are already too late."))); } /** * We can use the $currentCycle argument to balance workload, * or to execute low-priority actions every X cycle, or to * only make costly actions every X cycle */ if($currentCycle % 3 == 0){ $this->events->push(new Event("debug", Array("info" => "I am sent every 3 cycles"))); } } /** * This function will be called each cycle after $this->cycle() * This function must return a SSEServer\Events object containing all events (SSEServer\Event) which * should be sent as output in the current cycle. */ public function getEvents(): Events{ return $this->events->flush(); } /** * This function will be called each cycle after $this->getEvents() * Cleanup logic should be implemented here, e.g. unset variables stored * in this object which won't be needed at next cycle. In this case we left it blank. */ public function cleanUp(): Void{ } } //Create a new instance of our custom SSEControllerInterface object $controller = new ExampleController(); //Create a new SSEServer instance, pass the controller object as the first arg //Second arg is degubmode, if set to true, the server will send events with type "debug" //if there are any. $server = new SSEServer($controller, true); //Every $cycleTime seconds the server will do a cycle (default is 1 seconds) $cycleTime = 1; $server->setCycleTime($cycleTime); //In every $pingInterval (2nd) cycle a ping event will be sent //containing current time (milliseconds since epoch as int) (default is every cycle (1)) $pingInterval = 2; $server->setPingInterval(1); //Run the server (loop) $server->run();
Javascript
//Create EventSource pointing to the url of our php file const evtSource = new EventSource("events.php"); //Add event listener for any type of event which you implemented. evtSource.addEventListener("ping", (event) => { console.log(event.data); }); evtSource.addEventListener("message", (event) => { console.log(event.data); });
htrnf/sse-server 适用场景与选型建议
htrnf/sse-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 htrnf/sse-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 htrnf/sse-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-02