简体   繁体   English

NodeJS和ReactJS套接字通信

[英]NodeJS and ReactJS socket communication

I have a beautiful full stack app with NodeJS on the server side and ReactJS on the client side. 我有一个漂亮的全栈应用,服务器端有NodeJS,客户端有ReactJS。 The app relies on Socket.IO package, both Server and Client API. 该应用程序依赖于Server.Client API和Socket.IO软件包。

On my computer, everything works fine on localhost 4000 for node and localhost 3000 for react. 在我的计算机上,对于node的localhost 4000和对react的localhost 3000一切正常。 Perfect communication on every socket events. 在每个套接字事件上的完美通信。 But in the future, my set up will be a bit different. 但是将来,我的设置将有所不同。

I want to host the server side code on one server, called here SERVER_A, and the client side code on another server, called here SERVER_B, acting like a gateway for the users. 我想将服务器端代码托管在一个称为SERVER_A的服务器上,并将客户端代码托管在另一个称为SERVER_B的服务器上,充当用户的网关。 What I would expect is a client would open http://SERVER_B:80/myapp on he's personal computer, my react app would open and all of the socket communication would be done internally between SERVER_B and SERVER_A, leaving the client with the data emitted by the node app. 我期望的是客户端将在他的个人计算机上打开http://SERVER_B:80/myapp ,我的react应用将打开,并且所有套接字通信都将在SERVER_B和SERVER_A之间在内部完成,而客户端将释放数据通过节点应用程序。 But what really happens is, the user on he's personal computer needs to reach both port 80 of SERVER_B and port 4000 of SERVER_A for the entire app to run properly. 但是实际发生的是,他的个人计算机上的用户需要同时访问SERVER_B的端口80和SERVER_A的端口4000,才能使整个应用正常运行。

This is a bummer because this type of communication should be done under the hood. 这真是令人讨厌,因为这类交流应该在幕后进行。 Is there any way I can restrict the socket listen and emit events between Node and React and keep the client with the rendered data only? 有什么方法可以限制Node和React之间的套接字侦听和发出事件,并使客户端仅保留渲染的数据?

Hope this was clear and sorry for the noobness... :) 希望这很清楚,对不起....)

Thanks in advance! 提前致谢!

The default port a VirtualHost will respond with is configured "under the hood" (invisible to a first sight of a non-malicious consumer). VirtualHost将响应的默认端口是在“幕后”配置的(对非恶意使用者而言,它是看不见的)。 So your client will see https://SERVER_B/ and https://SERVER_A but the DEFAULT port will differe. 因此,您的客户端将看到https:// SERVER_B /https:// SERVER_A,但是DEFAULT端口将有所不同。

I think you want to make SERVER_A something like invisible to the user.Instead under the hood it serves SERVER_B for the data. 我认为您想使SERVER_A对用户不可见,而是在后台为数据提供SERVER_B。 If its so I think you have to setup a virtual host in SERVER_B (use something like proxy) to communicate between SERVER_B and SERVER_A.So whenever your client makes request to the SERVER_B(for connection associated to node app)the SERVER_B will make socket connection to the SERVER_A and the communication proceeds. 如果是这样,我认为您必须在SERVER_B中设置一个虚拟主机(使用代理之类的东西)以在SERVER_B和SERVER_A之间进行通信。因此,只要您的客户端向SERVER_B发出请求(用于与节点应用程序关联的连接),SERVER_B就会进行套接字连接到SERVER_A,然后通信继续。

If I have to say I would not recommend to do so beacause these sort of setup will degrade the performace.The reason behind this is, SERVER_B has to first read the client streams then write the streams to SERVER_A.When SERVER_A sends some streams it has to again write back to the client. 如果我不得不说我不建议这样做,因为这种设置会降低性能。背后的原因是,SERVER_B必须先读取客户端流,然后将流写入SERVER_A。当SERVER_A发送一些流时,再次写回客户端。

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

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