I am using JavaEE 7 and Glassfish 4 as my project setup and I would like to use websockets. As browser I have used Chrome 35.0.1916.153. My endpoint should receive a message through websockets and broadcast this message to other users within the same group.
If I open two browser tabs which open websocket connections to different groups [e.g. first 1, then 2 (the order matters!)] everything works as expected. But when I call the broadcast method by the first tab (groupId=1) the groupId path parameter has the value 2. It looks like it is the last onOpen call's value.
In this call the Session object looks like this:
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'
}
}
The URI from session and endpoint differ which may result in the unwanted behavior.
Has anyone noted this yet?
My endpoint class looks like this:
@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);
}
}