« Return to Thread: Same page different params!

Re: Same page different params!

by Cameron Braid :: Rate this Message:

Reply to Author | View in Thread

Here's a rough class (typed in my mail program and therefore not compiled, let alone tested) that could do this for you.

There is room for improvement on many fronts, but it gives the basic idea.

i.e. There may be a more elegant way to detect when script sessions has been invalidated.

class RoomManager {

  Map<String, List<ScriptSession>> subscriptions = new HashMap<String, Set<ScriptSession>>();

  public syncronized void connect(String room) {
    Set<ScriptSession> set = subscriptions.get(room);
    if (set == null) {
        set =  new HashSet<ScriptSession>();
        subscriptions.put(room, set);
    }
    set.add( WebContextFactory.get().getScriptSession() );
  }

  public syncronized void disconnect(String room) {
    Set<ScriptSession> set = subscriptions.get(room);
    if (set != null) set.remove( WebContextFactory.get().getScriptSession() );
  }

  private syncronized void sendScriptToRoom(ScriptBuffer script, String room) {

    // should probably use an iterator here, and remove the scriptSession from the room if it is invalidated

     for (ScriptSession scrpitSession : rooms.get(room)) {
        if (!scrpitSession.isInvalidated()) {
             scrpitSession.addScript(script);
        }

    }
  }



// your method to generate the ScriptBuffer to send via sendScriptToRoom

  public void sendMessage(String room, String message) {

   ScriptBuffer script = new ScriptBuffer();
    script.appendScript("onMessage(");
    script.appendData(message");
    script.appendScript(")");
    sendScriptToRoom(room, script);
  }
}

Then, in your JSP, you can do something like this :
<head>
    <script>
        var roomID = "<%=StringUtils.escapeJavaScript(request.getParameter("roomID"))%>";
       onMessage = function(message) {
          alert(message)
      }
    </script>
</head>
<body onload='RoomManager.connect(roomID); RoomManager.sendMessage(roomID, "hello world");'  onunload='RoomManager.disconnect(roomID)'
>

</body>

On Mon, 2007-09-17 at 05:39 -0700, stojadinovicp wrote:
:confused:

Hi everyone!

In short, the problem is the following. Its an online card game with Rooms
and Tables organization. Now, one JSP is used to opened different rooms,
naturally. Same goes for tables. Depending on the params passed, like
"room/index.jsp?roomID=NewOrleans" for example, and then another room is
accessed by "room/index.jsp?roomID=BabyBlue"

The problem is that when I use:
	String currentPage = wctx.getCurrentPage();
	Collection<ScriptSession> sessions =
wctx.getScriptSessionsByPage(currentPage);
	for (ScriptSession ss : sessions) ss.addScript(new ScriptBuffer(js));

then ALL rooms get ALL messages :(((
So, if a user chats in one room, this goes to all the rooms :(((

It does make sense to see "room/index.jsp" as one page regardless of the
"?roomID=BlaBla" part, but therein lies my problem :(

Now, one obvious solution is to include the roomID in the JavaScript call
and then if the roomID is not valid I ignore the message. BUT, this sucks
big time because the idea is to have a few rooms but each of these should
have many tables opened at the same time. So there will be a LOT of messages
flying around and being ignored. In other words, one table page will get ALL
messages from ALL tables in ALL rooms.

I would REALLY like to avoid sending THIS much data to my users :(

Any ideas how to solve this? PLEASE!
ANY idea is welcome! And I do have a deadline :(((

THANKS!!!

 « Return to Thread: Same page different params!

LightInTheBox - Buy quality products at wholesale price