简体   繁体   English

刷新后保持 WebSocket 连接处于活动状态

[英]Keep WebSocket connection alive after refresh

I have a real time application which used WebSockets between java spring server and browser.我有一个实时应用程序,它在 java spring 服务器和浏览器之间使用 WebSockets。 Is there a method to keep the Websocket connection alive after page refresh ?是否有一种方法可以在页面刷新后保持 Websocket 连接处于活动状态?

This is my javascript code:这是我的 javascript 代码:

consumerWebSocket = new WebSocket("wss://" + window.location.host + 
"/myWebSocketConnection");

consumerWebSocket.onopen = function () {
    sendThroughWS(consumerWebSocket, "Send this message from browser to server");

};

No, you cannot. 你不能。 The problem is that the context a websockets lives in is limited to the page you are displaying. 问题是websocket所处的上下文仅限于您显示的页面。 As soon as you refresh the page or navigate to another, the context (and therefore the websocket) is destroyed. 刷新页面或导航到另一个页面后,上下文(因此是websocket)将被破坏。

A solution to prevent destroying is to create some sort of Single page Application. 防止破坏的解决方案是创建某种单页应用程序。 The new content would be loaded into the existing page and the websocket is not reloaded. 新内容将被加载到现有页面中,并且不会重新加载websocket。

Another way is using frames. 另一种方法是使用框架。 You can put your Websocket in a (hidden) frame and just navigate using another frame. 您可以将Websocket放在一个(隐藏的)框架中,然后仅使用另一个框架进行导航。 This way the socket can be stay alive and you can navigate through the rest of your page without a websocket reload. 这样,套接字可以保持活动状态,并且您可以浏览页面的其余部分而无需重新加载websocket。

1. On every refresh new socket connection will be created the problem is not new socket but will the rest of the application use this new socket connection to communicate with your client or a previous connection?? 1. 每次刷新都会创建新的套接字连接,问题不是新的套接字而是应用程序的其余部分是否会使用这个新的套接字连接与您的客户端或以前的连接进行通信?

So this above is the riddle to solve , to tell the rest of the application that Hey: forget my last connection instance, use this new one instead所以以上就是要解决的谜语,告诉应用程序的其余部分嘿:忘记我的最后一个连接实例,改用这个新的连接实例

  1. Communications on your chat application or whatever should be persisted on database with your users id or users/friend relationship you've chosen您的聊天应用程序或任何应该与您选择的用户 ID 或用户/朋友关系保存在数据库中的通信

  2. Sometimes its important to store reference of your socket connection to some sort of persistence layer Redis might work, even on other database its okey.有时,重要的是将套接字连接的引用存储到某种持久层 Redis 可能工作,即使在其他数据库上也是如此。 on close of connection mark that instance reference as close on refresh so you have new socket, then update the socket instance reference on your persistence layer( some sort of db )在连接关闭时将该实例引用标记为关闭时刷新,以便您拥有新的套接字,然后更新持久层上的套接字实例引用(某种 db )

  3. Once your application wants to send message to you it go through the db and if they find the open reference to your socket connection then the socket is used to send you a message if no reference or closed, then the text/messages are stored on database.一旦您的应用程序想要向您发送消息,它就会通过数据库,如果他们找到对您的套接字连接的开放引用,那么如果没有引用或关闭,则使用套接字向您发送消息,然后文本/消息存储在数据库中.

  4. When you've refreshed a webpage or new socket is connected then the client side browser should request the chats/message from database from backend, through that very socket => Mind some pagination, not to load tones of text on one request当您刷新网页或连接新套接字时,客户端浏览器应通过该套接字从后端请求来自数据库的聊天/消息 => 注意分页,不要在一个请求时加载文本音调

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

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