Ratchet Websocket推送集成

时间:2017-07-27 15:47:52

标签: php networking websocket ratchet

我正在尝试使用Websockets(Ratchet)开发聊天系统。到目前为止,我已经制作了一个基于PHP的功能性Websocket服务器,它能够使用以下方法回答预定义的JSON编码消息。

function onMessage(ConnectionInterface $from, $msg){ ... }

问题是我想通过后台工作者/线程将消息从后端数据库推送到正确的客户端,如果我的表中的内容发生了变化。这可能使用PHP吗?

我不希望客户端每5分钟询问一次websocket服务器来刷新其状态,如下所示。

{"action":"giveMeUpdates"}

但是网络服务器应该能够做到这样的事情:

{"status":"newMessages", "messagelist":[...]}

另外:

class Chat extends ChatActionHandler implements MessageComponentInterface { ... }

这是我的类,其中ChatActionHandler包含与客户端请求交互的函数。使用MessageComponentInterface,我只能回复像function onOpen(ConnectionInterface $conn)这样的Socket事件。它由RatchetIO服务器运行:

$server = IoServer::factory(
   new Chat(),
   8080);
$server->run();

1 个答案:

答案 0 :(得分:0)

您可以附加计时器,例如cron和

{{1}}