简体   繁体   English

Ratchet PHP - 推送消息服务

[英]Ratchet PHP - Push messaging service

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.我使用 Ratchet 查看的大多数示例都用于聊天服务,我目前正在构建一个应用程序,用户可以在其中登录并根据其用户 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:onOpen()方法被调用时,我将$conn对象设置为一个数组,如下所示:

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

In my Javascript, within the onopen function I also send a JSON payload with the send function.在我的 Javascript 中,在onopen函数中,我还使用send函数发送了一个 JSON 有效负载。 My server pics this up and stores it like so, in the onMessage method:我的服务器将它记录下来并像这样存储在onMessage方法中:

$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?我查看了onSubscribebroadcast无济于事,但真的不明白WampServerInterface应该用于什么?

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.使用另一个脚本获取当前连接的用户,在某处处理这些用户并返回他们可能拥有的任何更新,每 60 秒一次。 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?那么我如何才能将我正在运行的服务器脚本与其他地方的其他“静态”PHP 脚本进行交互呢?

Thanks谢谢

The $conn parameter is a implementation of the ConnectionInterface - it has a send() method. $conn参数是ConnectionInterface 的一个实现——它有一个send()方法。 So that's how you can send messages to the client.这就是您可以向客户端发送消息的方式。

Ratchet (via React.PHP) also supports timers . Ratchet(通过 React.PHP)也支持 timers So if there's no external dependency, you can just use $loop->addPeriodicTimer() to send messages to every client every 60 seconds.所以如果没有外部依赖,你可以只使用$loop->addPeriodicTimer()每 60 秒向每个客户端发送一次消息。

If you need to send messages based on some external dependencies (like a web server request or a cron script), use ZMQ ( Ratchet docs ).如果您需要根据某些外部依赖项(如 Web 服务器请求或 cron 脚本)发送消息,请使用 ZMQ( Ratchet 文档)。 It's easy.这很简单。

Check out my slides about WebSockets in PHP .查看我关于 PHP 中的 WebSockets 的幻灯片 The notes are in Czech, but you can find the source code examples useful.注释是捷克语,但您可以找到有用的源代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM