Java 为什么getOpenSessions()返回0
我正在尝试用java和tomcat创建一个聊天室。这里的Java 为什么getOpenSessions()返回0,java,websocket,Java,Websocket,我正在尝试用java和tomcat创建一个聊天室。这里的id是聊天室id。每次我连接一个新用户,但是getOpenSessions()函数返回0。 每个客户端都使用以下uri ws://localhost:8080/websocket_implementation/chat/123 以下是打开连接的代码 @OnOpen public void open(final Session session, @PathParam("id") final String id) {
id
是聊天室id。每次我连接一个新用户,但是getOpenSessions()
函数返回0。
每个客户端都使用以下uri
ws://localhost:8080/websocket_implementation/chat/123
以下是打开连接的代码
@OnOpen
public void open(final Session session, @PathParam("id") final String id) {
System.out.println(session.getUserProperties());
session.getUserProperties().put("id", id);
System.out.println(session.getOpenSessions().size());
}
你知道我做错了什么吗?我也遇到过同样的问题(Tomcat 7、Oracle Java8),它可以在本地主机上的windows上运行,但当部署在安装了Ubuntu的远程计算机上时,getOpenSessions()返回0
如果没有人知道如何修复它,则应手动管理所有打开的会话,例如:
private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
private static final Set sessions=Collections.synchronizedSet(new HashSet());
将会话添加到集合onOpen和移除onСlose
Amazon EC2 Ubuntu Server 14.04 LTS
When the app is deployed on Tomcat 7, getOpenSessions() returns 0
when deployed on jetty 9 getOpenSessions() works as expected.
看起来像是个问题