Ratchet Websocket 推送集成
Ratchet Websocket push integration
我正在尝试使用 Websockets (Ratchet) 开发一个聊天系统。到目前为止,我已经制作了一个基于 PHP 的功能性 Websocket 服务器,它能够使用以下方法回答预定义的 JSON 编码消息。
function onMessage(ConnectionInterface $from, $msg){ ... }
问题是,如果我的表中发生某些更改,我想通过后台 worker/thread 将消息从后端数据库推送到正确的客户端。这可能使用 PHP 吗?
我不希望客户端每 5 分钟要求 websocket 服务器刷新其状态,如下所示。
{"action":"giveMeUpdates"}
但是网络服务器应该能够做这样的事情:
{"status":"newMessages", "messagelist":[...]}
另外:
class Chat extends ChatActionHandler implements MessageComponentInterface { ... }
这是我的 class,其中 ChatActionHandler 包含与客户端请求交互的功能。使用 MessageComponentInterface,我只能回复 function onOpen(ConnectionInterface $conn)
这样的套接字事件。它由 RatchetIO 服务器运行:
$server = IoServer::factory(
new Chat(),
8080);
$server->run();
你可以附加定时器,比如 cron 和
$this->loop->addPeriodicTimer($timeout, function($timer) {});
我正在尝试使用 Websockets (Ratchet) 开发一个聊天系统。到目前为止,我已经制作了一个基于 PHP 的功能性 Websocket 服务器,它能够使用以下方法回答预定义的 JSON 编码消息。
function onMessage(ConnectionInterface $from, $msg){ ... }
问题是,如果我的表中发生某些更改,我想通过后台 worker/thread 将消息从后端数据库推送到正确的客户端。这可能使用 PHP 吗?
我不希望客户端每 5 分钟要求 websocket 服务器刷新其状态,如下所示。
{"action":"giveMeUpdates"}
但是网络服务器应该能够做这样的事情:
{"status":"newMessages", "messagelist":[...]}
另外:
class Chat extends ChatActionHandler implements MessageComponentInterface { ... }
这是我的 class,其中 ChatActionHandler 包含与客户端请求交互的功能。使用 MessageComponentInterface,我只能回复 function onOpen(ConnectionInterface $conn)
这样的套接字事件。它由 RatchetIO 服务器运行:
$server = IoServer::factory(
new Chat(),
8080);
$server->run();
你可以附加定时器,比如 cron 和
$this->loop->addPeriodicTimer($timeout, function($timer) {});