PHP套接字连接到TCP服务器

时间:2023-02-10 18:55:44

I have the following scenario:

我有以下情况:

I have a given program which provides a JSON interface to which I can connect using a socket connection. Since I want to integrate that interface into my web-application, I'm trying to use the PHP sockets for the communication between server and client. The communication is bidirectional, which means my PHP client is sending requests to the server and the server is also sending requests to my PHP "client". I have no problems with the connection between my PHP and the JSON interface. The only problem is, since I have to wait for requests on the PHP side, I have to run it in an infinity loop. I want to 'echo' some responses and requests i get somewhere into my web-application without having that infinity loop.

我有一个给定的程序,它提供了一个JSON接口,我可以使用套接字连接。由于我想将该接口集成到我的web应用程序中,我正在尝试使用PHP套接字进行服务器和客户端之间的通信。通信是双向的,这意味着我的PHP客户端正在向服务器发送请求,服务器也向我的PHP“客户端”发送请求。我的PHP和JSON接口之间的连接没有问题。唯一的问题是,因为我必须等待PHP端的请求,我必须在无限循环中运行它。我希望'回应'一些响应和请求我进入我的网络应用程序而没有无限循环。

My question is, is there a good way to create one php file which can:

我的问题是,是否有一个很好的方法来创建一个PHP文件,它可以:

  • create an own socket server so I can send stuff to it from my web application without being stuck in an infinity loop
  • 创建一个自己的套接字服务器,这样我就可以从我的Web应用程序发送内容而不会陷入无限循环

  • the stuff I sent to it can be redirected to the JSON server
  • 我发送给它的东西可以重定向到JSON服务器

  • the response I get from my JSON server redirecting to my web application
  • 我从我的JSON服务器重定向到我的Web应用程序的响应

Use case I have a solution for: I have a NFC card reader which provides me the functions and informations of a card (uniqueid) and it's connected to my network. The JSON server sends me a request on "card detected" and I respond with "allowed" or "not allowed". (There the infinity loop doesn't matter)

用例我有一个解决方案:我有一个NFC读卡器,它提供了卡(uniqueid)的功能和信息,并且它连接到我的网络。 JSON服务器向我发送“卡检测到”的请求,我回复“允许”或“不允许”。 (那里的无限循环并不重要)

Use case I don't have a solution for: I have my web application open and I want to write the "uniqueid" parameter into an input field to assign that card to a person. I want to do it this way: - Click a button "assign card" - Hold card over the card reader - Write uniqueid into input field

用例我没有解决方案:我打开了我的Web应用程序,并且我想将“uniqueid”参数写入输入字段以将该卡分配给某个人。我想这样做: - 单击“分配卡”按钮 - 将卡保持在读卡器上 - 将唯一ID写入输入字段

I don't want to make a direct connection from the web application to the JSON server. I want to make a temporary connection from the web application to the PHP server which has a permanent connection to the JSON server.

我不想直接从Web应用程序连接到JSON服务器。我想从Web应用程序到PHP服务器建立临时连接,该服务器与JSON服务器具有永久连接。

I hope this is understandable.

我希望这是可以理解的。

1 个解决方案

#1


1  

Yes you can. Look into using Ratchet in your application. It seems to fit your requirements. It has bi-directional communication via Websockets.

是的你可以。在您的应用程序中查看使用Ratchet。它似乎符合您的要求。它通过Websockets进行双向通信。

Your browser will connect to a Ratchet based application in your server listening in a certain port and you will be able to send and receive messages using that connection.

您的浏览器将连接到服务器中基于Ratchet的应用程序,并在某个端口进行侦听,您将能够使用该连接发送和接收消息。

The alternative is long-polling. You can learn more in this * answer (which also features Websockets).

另一种选择是长轮询。您可以在此*答案(其中还包含Websockets)中了解更多信息。

#1


1  

Yes you can. Look into using Ratchet in your application. It seems to fit your requirements. It has bi-directional communication via Websockets.

是的你可以。在您的应用程序中查看使用Ratchet。它似乎符合您的要求。它通过Websockets进行双向通信。

Your browser will connect to a Ratchet based application in your server listening in a certain port and you will be able to send and receive messages using that connection.

您的浏览器将连接到服务器中基于Ratchet的应用程序,并在某个端口进行侦听,您将能够使用该连接发送和接收消息。

The alternative is long-polling. You can learn more in this * answer (which also features Websockets).

另一种选择是长轮询。您可以在此*答案(其中还包含Websockets)中了解更多信息。