我使用JavaEE 7和Glassfish 4作为我的项目设置,我想使用websockets。作为浏览器,我使用Chrome 35.0.1916.153。我的端点应该通过websockets接收消息,并将此消息广播给同一组内的其他用户。
如果我打开两个浏览器选项卡,打开websocket连接到不同的组[例如,先1,然后2(顺序很重要!)],一切都像预期的那样工作。但是,当我通过第一个选项卡(groupId=1)调用广播方法时,groupId路径参数的值为2。它看起来像是最后一个onOpen调用的值。
在这个调用中,Session对象看起来像这样:
session = (org.glassfish.tyrus.core.SessionImpl) SessionImpl{
uri=/test/group/1, <--
id='0fcff8ae-7ff8-42e8-a9e9-03cbb1f62562',
endpoint=EndpointWrapper{
endpointClass=null,
endpoint=org.glassfish.tyrus.core.AnnotatedEndpoint@59d91963,
uri='/test/group/2', <--
contextPath='/test'
}
}
来自会话和端点的URI不同,这可能导致不想要的行为。
有人注意到这一点吗?
我的端点类看起来像这样:
@ServerEndpoint(value = "/group/{groupId}",
encoders = {MessageEncoder.class},
decoders = {MessageDecoder.class})
public class GroupEndpoint {
private Map<String, Set<Session>> groups = GroupSingleton.getGroups();
@OnMessage
public void broadcast(@PathParam("groupId") String groupId,
Message message,
Session session)
throws IOException, EncodeException {
for (Session session : groups.get(groupId))
session.getBasicRemote().sendObject(message);
}
@OnOpen
public void onOpen(@PathParam("groupId") String groupId,
Session session){
if(groups.containsKey(property)){
// then add peer to the list of viewers
groups.get(groupId).add(session);
}else{
// if not start a new list
Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
sessions.add(session);
groups.put(groupId, sessions);
}
}
@OnClose
public void onClose(@PathParam("groupId") String groupId, Session session) {
groups.get(groupId).remove(session);
if(groups.get(groupId).isEmpty())
groups.remove(groupId);
}
}
这是一个已知的错误,跟踪为Tyrus -203,已在Tyrus 1.1中修复
你可以使用一个提升的Glassfish构建吗?
谢谢,帕维尔