简体   繁体   English

WEB SOCKET PHP概念

[英]WEB SOCKET PHP concept

I want to keep a persist connection between my server and clients (android app) so I can push data from server to my clients. 我想在我的服务器和客户端(Android应用程序)之间保持持久连接,这样我就可以将数据从服务器推送到我的客户端。 After some search I found that the best way to do is WEB SOCKET. 经过一番搜索,我发现最好的办法是WEB SOCKET。 But there is two scenarios in here: 但这里有两种情况:

first is, I need to sent some data (command) like broad cast to some of my clients (not all) and then listen for their reply. 首先,我需要向我的一些客户(不是所有客户)发送一些像广播这样的数据(命令),然后听取他们的回复。 And second is, I need to sent a notify to some of clients. 第二,我需要向一些客户发送通知。

It's like chat room that there is a general room that the messages can be seen by everybody in room and some private rooms that messages can be seen just by two users who participate in chat. 这就像聊天室那样,有一个普通的房间,房间里的每个人都可以看到这些消息,而一些私人房间的消息只能由参与聊天的两个用户看到。

I saw some example code but I couldn't understand the different between those two scenarios in codes. 我看到了一些示例代码,但我无法理解代码中这两种情况之间的差异。 I need also some information about ZeroMQ and does it worth to use ZeroMQ for the project or not? 我还需要一些关于ZeroMQ的信息,是否值得为项目使用ZeroMQ?

Just some links of references would be appreciated. 只是一些参考链接将不胜感激。

EDIT 编辑

I saw in code that people define some infinite loop to check some event but my idea is to create a virtual client in server that can call by other function so I don't need to change anything in DB and then check for event in my loop. 我在代码中看到人们定义了一些无限循环来检查某个事件但我的想法是在服务器中创建一个可以通过其他函数调用的虚拟客户端,所以我不需要在DB中更改任何内容然后在我的循环中检查事件。 the event can call this virtual client that can sent my command in broad cast. 该事件可以调用此虚拟客户端,该客户端可以在广播中发送我的命令。 is that a proper way to do that? 这是一个正确的方法吗?

Use Ratchet for this, It is a PHP library providing developers with tools to create real time, bi-directional applications between clients and servers over WebSockets. 使用Ratchet ,它是一个PHP库,为开发人员提供工具,通过WebSockets在客户端和服务器之间创建实时,双向应用程序。

You can use it's subscribe/unsubscribe (Topics) feature, which will fulfill your needs to send push notification in general and to specific users. 您可以使用它的订阅/取消订阅(主题)功能,该功能将满足您一般发送推送通知和特定用户的需求。 Create different topics for them. 为他们创建不同的主题。

eg One topic would be general for which every user will get registered on page load and one would be for specific users. 例如,一个主题是通用的,每个用户将在页面加载时注册,一个用于特定用户。 You can create as many as topics you need. 您可以创建所需主题。

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

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