简体   繁体   English

从服务器端向客户端发送“消息”

[英]Sending a “message” from server-side to client-side

We developed a web application (HTML/CSS/JavaScript on client-side, web services on server-side). 我们开发了一个Web应用程序(客户端上的HTML / CSS / JavaScript,服务器端上的Web服务)。 Each user action on client-side calls to some web method on server-side. 客户端上的每个用户操作都会在服务器端调用某些Web方法。

But we have some problems when user opens more than one browser tab with our application, so we decided to force logout in all browser's tabs (excluding the current tab) each time when user login in different browser's tab. 但是,当用户使用我们的应用程序打开多个浏览器选项卡时,我们会遇到一些问题,因此我们决定每次用户登录不同浏览器的选项卡时都强制注销所有浏览器的选项卡(当前选项卡除外)。 How we could implement that client-side will be able to receive "messages" from the server-side? 我们如何实现客户端将能够从服务器端接收“消息”?

When you say sending a message from server-side to client-side I assume that you don't want a response on Node.js like: 当您说从服务器端向客户端发送消息时,我假设您不希望对Node.js做出响应,例如:

res.status(200).send('Hello World') don't you? res.status(200).send('Hello World')是吗?

So, what you need is using websockets. 因此,您需要使用websockets。 I recommend you to use socket.io 我建议您使用socket.io

You only need to send a message with a socket on your server side and listen for that message on your client side. 您只需要在服务器端发送带有套接字的消息,并在客户端监听该消息。

Using websockets you can send a message to each user identified with an Id, or sending message in a broadcast. 使用websocket,您可以向标识有ID的每个用户发送消息,或通过广播发送消息。 There are a lot of possibilities with websockests. websockests有很多可能性。

You can find examples on the web using websockets. 您可以使用websockets在网络上找到示例。 I think that is what you are looking for. 我认为这就是您想要的。

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

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