【问题标题】:Ratchet Websocket push integrationRatchet Websocket 推送集成
【发布时间】:2017-07-27 15:47:52
【问题描述】:

我正在尝试使用 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) 这样的套接字事件。它由 RatchetIO 服务器运行:

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

【问题讨论】:

    标签: php networking websocket ratchet


    【解决方案1】:

    你可以附加定时器,比如 cron

    $this->loop->addPeriodicTimer($timeout, function($timer) {});
    

    【讨论】:

    • MessageComponentInterface 没有这样的功能。你的意思是 \React\EventLoop\LoopInterface 吗?我想我可能必须首先在 Chat 类的构造函数中的某个地方实例化它,对吧?
    猜你喜欢
    • 2014-12-12
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2021-06-18
    相关资源
    最近更新 更多