In case anyone reads this and has the same problem here is one solution.
Since I already had some user management I simply extended it in the following way. When a user opens the page there is a call to userEntered method. In here I create an instance of UserSession which has two members, username and ScriptSession. ScriptSession you get from wctx.getScriptSession() (this is from the article above) This UserSession is then added to List<UserSession> sessions.
From then on I send messages with
for (UserSession s : sessions) s.addScript(js);
and thats it. (the addScript method has one line: ss.addScript(new ScriptBuffer(js));)
Now, I also have userLeft method which gets called on page onunload event or on a user ping timeout. (users send a ping every few seconds constantly)
in userLeft method I simply remove the UserSession from sessions List and thats it.
Tested on two rooms, works fine. |