java ee工程窗口关闭,JavaEE 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 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.

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?

Thanks

解决方案

Instead of using

s.getBasicRemote().sendText(TwitterHoseBird.getMsgQueue().take());

change it to

s.getAsyncRemote().sendText(TwitterHoseBird.getMsgQueue().take());

and everything else would just workout fine

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值