本文介绍了JavaEE Websocket:关闭浏览器选项卡会关闭所有会话而不管浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个websocket端点
I have a websocket endpoint as
@ServerEndpoint("/tweets")
public class TweetStreamServer {
private static final Logger LOGGER = LoggerFactory.getLogger(TweetStreamServer.class);
@OnMessage
public void tweets(final String message, final Session session) throws IOException, InterruptedException {
System.out.println("session id:" + session.getId() + ", search term: " + message);
final Client twitterClient = TwitterHoseBird.getInstance(message);
while (!session.getOpenSessions().isEmpty()) {
for (final Session s : session.getOpenSessions()) {
if (twitterClient.isDone()) {
System.out.println("Twitter Client Done, waiting ...");
}
s.getBasicRemote().sendText(TwitterHoseBird.getMsgQueue().take());
}
}
}
}
I在 WildFly 8.1.0 Final
上部署它。然后我在 Chrome
, Safari
上打开多个标签,然后运行以下内容
I deploy this on WildFly 8.1.0 Final
. Then I open multiple tabs on Chrome
, Safari
and run the following
var connection = new WebSocket('ws://127.0.0.1:8080/tweetstream-1.0-SNAPSHOT/tweets');
connection.onopen = function () {
connection.send('germany');
};
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
connection.onclose = function (e) {
console.log('closing session');
};
然后所有标签开始从服务器接收数据。
Then all the tabs start receiving data from server.
- 然后当我在其中一个标签上执行
connection.close();
时,只有那个连接中断,而所有其他标签都是仍然接收数据 - 但如果我关闭其中一个标签(在任何浏览器中),则所有其他标签中打开的所有会话都会以
关闭会话会话
消息
- Then when I do
connection.close();
on one of the tabs, only that connection breaks while all the other tabs are still receiving the data - But if I close one of the tabs (in any browser), all the sessions that were open in all the other tabs close session with
closing session
message
问题
- 是吗不是一个有效的用例,如果用户在一个浏览器中关闭选项卡,所有其他选项卡仍然应该接收数据?
- 您是否看到我正在做的任何错误/问题?
- 我该如何解决这个问题?
Question
- Is it not a valid use case that if user closes a tab in one browser, all the other tabs should still receive the data?
- Do you see any bug/issue with what I am doing?
- How can I fix this issue?
谢谢
推荐答案
而不是使用
s.getBasicRemote().sendText(TwitterHoseBird.getMsgQueue().take());
将其更改为
s.getAsyncRemote().sendText(TwitterHoseBird.getMsgQueue().take());
其他一切都可以正常锻炼
and everything else would just workout fine
这篇关于JavaEE Websocket:关闭浏览器选项卡会关闭所有会话而不管浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!