这是我的Java
端点
@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());
}
}
}
@OnClose
public void onClose(Session session, CloseReason reason) throws IOException {
LOGGER.warn("closing session: {}, remaining session: {}", session.getId(), session.getOpenSessions().size());
}
在Chrome上,我在4个不同的tabs
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');
};
然后,所有标签都会从Twitter接收数据。
如果我在其中一个标签上执行connection.close();
,则只有该标签关闭连接,但其他3标签仍会接收数据。
但是,如果我关闭标签,则关闭所有其他标签中的连接,方法是记录closing session
这是什么问题?
在服务器上,当我关闭标签时,我会将日志视为
[0m[33m09:24:53,342 WARN [com.self.tweetstream.TweetStreamServer] (default task-16) closing session: tulDjtq4Lx8s1Zo3mA2mwX8B, remaining session: 1
即使关闭了任何标签,如何才能进行其他连接?