duanlie2709 2014-05-26 12:47
浏览 28
已采纳

棘轮PHP - 推送消息服务

Most of the examples I'm looking at with Ratchet are for chat services, and I'm currently building an application where the user logs in and receives notifications from the server based on their user ID.

I have the basic tutorials working, however I'm struggling to understand a few points:

When the onOpen() method is called, I set the $conn object into an array like so:

$this->clients[$conn->resourceId]['conn'] = $conn;

In my Javascript, within the onopen function I also send a JSON payload with the send function. My server pics this up and stores it like so, in the onMessage method:

$this->clients[$conn->resourceId]['json'] = $json;

So now I have my connected users stored in an array, how do I send a message to a specific user? I've looked into onSubscribe and the broadcast to no avail, but don't really understand what the WampServerInterface is supposed to be used for?

Since my own class is a running script, I obviously can't create a new instance of it anywhere else. As my application will be sending user specific updates I need some sort of way to do this following:

Grab the currently connected users using another script, process these somewhere and return any updates they might have, every 60 seconds. Now although this might sound like polling, it would only be one connection doing this and so wouldn't be intensive on the server - at least that's what I think. So how can I interact my running server script with other "static" PHP scripts elsewhere?

Thanks

  • 写回答

1条回答 默认 最新

  • doubi4531 2014-05-26 12:56
    关注

    The $conn parameter is a implementation of the ConnectionInterface - it has a send() method. So that's how you can send messages to the client.

    Ratchet (via React.PHP) also supports timers. So if there's no external dependency, you can just use $loop->addPeriodicTimer() to send messages to every client every 60 seconds.

    If you need to send messages based on some external dependencies (like a web server request or a cron script), use ZMQ (Ratchet docs). It's easy.

    Check out my slides about WebSockets in PHP. The notes are in Czech, but you can find the source code examples useful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题