provm/events
Composer 安装命令:
composer create-project provm/events
包简介
Event handler for WebSocket
README 文档
README
WebSockets app for PHP. Using Ratchet as a base for creating the app it can be used as framework for connecting to the database from the web UI.
Introduction
I wrote this project because I had a hard time finding any alternative to handling Server-Side WebSocket with PHP. Ratchet handles the connection but not the app itself. I needed an app structure similar to Slim which was what I work with. This is the result.
Installation
Use composer.
composer require aldarien/events
Use
I recommend you setup your app with a Container like PHP-DI
Base example:
$app = ProVM\Common\Alias\Server\App::factory(
new Ratchet\Server\HttpServer(
new Ratchet\Server\WsServer(
new ProVM\Common\Alias\Event\Message(new SplObjectStorage())
)
),
$port ?? 8010
);
$app->add('event_name', function($request, $response) {
// do action
return $response;
})
$app->run();
Breakdown:
The app is created similar to Ratchet with the wrapper ProVM\Common\Alias\Server\App but the message handler is from this package ProVM\Common\Alias\Event\Message which handles all event dispatches and listeners.
Then the event(s) are added with $app->add it requires a name for the event and a callable handler that work similar to Slim Controllers.
Finally just $app->run
You can use a Listener for the callable:
class Handler {
public function __invoke($request, $response) {
// do something else
return $response;
}
public function other($request, $response) {
// other action
return $response;
}
}
$app->add('event2', new Handler());
$app->add('other', [new Handler(), 'other']);
In the web UI just create a websocket:
var conn = new WebSocket(websocket_url)
conn.onopen = function(e) {
// open event
}
conn.onmessage = function(e) {
// listen to messages received
}
conn.onerror = function(e) {
console.error(e)
}
conn.onclose = function(e) {
// check if e.code == 1000 closed without error)
}
msg = {
action: 'event_name',
data: []
}
conn.send(JSON.stringify(msg))
Breakdown:
Create the websocket connection new WebSocket(websocket_url) where websocket_url can be something like ws://localhost:8010 or wss://localhost:8010 where the port is the same one set in the app.
Set the event listeners for open, message, error and close and start sending messages with send
Take note of the last lines where the message received by the app needs an action equivalent to the event names added before and the data associated if any (can be missing).
provm/events 适用场景与选型建议
provm/events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 provm/events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 provm/events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-30