[SOLVED] Handling clients leaving rooms

All Union Platform questions, comments, feature requests, and bug reports.

Handling clients leaving rooms  

Postby stevemann » Fri 24 Aug 2012 10:04

I need to write a room attribute logging the time a client leaves that room and I'm doing it by having a Flash admin app running constantly logged into the relevant room and this handles the CLIENT_DISCONNECTED event from ClientManager, subsequently writing the attribute.

Thing is I don't want to have to rely on having an app always running for the corresponding room and would much rather put all this into a module. The rooms in the system are all created dynamically which excludes the possibility of writing room modules - which leaves server modules. I'm not a java coder but have followed the procedures documented here and successfully got a server module loading on server start. I think I can now handle java syntax well enough but I still don't know what technique I would use. I can't think how I can listen for client removal from any existing room and then write an attribute to the room that client just left.

Any tips?

-Steve
stevemann
 
Posts: 20
Joined: Sat 26 May 2012 02:32

Re: Handling clients leaving rooms

Postby nyles33 » Mon 24 Sep 2012 08:05

You can create room modules and create them from the client side. One of my previous questions was to get an example in js - you can look at it to get a full example, but, in short, you would use something like when you create the room.

msgManager.sendUPC(UPC.CREATE_ROOM, "yourRoomName", null, null, "class|yourPackageName.ClassName");
nyles33
 
Posts: 13
Joined: Thu 05 Jul 2012 05:39

Re: Handling clients leaving rooms

Postby adkebab » Mon 24 Sep 2012 16:16

Hi Steve,

From the docs I can see that the server object supports these events: ROOM_CREATED, ROOM_REMOVED.

Using these you can manage a list of active rooms. In your server module create an event listener for the ROOM_CREATED and ROOM_REMOVED events. When the ROOM_CREATED event fires you attach an event listener to the new room for the roomEvent REMOVE_CLIENT and when ROOM_REMOVED fires you just remove the event listener that you created earlier for the roomEvent REMOVE_CLIENT.

If you are interested I can put together a small example.

Cheers,

Aart
adkebab
 
Posts: 2
Joined: Thu 13 Sep 2012 06:19

Re: Handling clients leaving rooms

Postby stevemann » Mon 08 Oct 2012 07:17

Many thanks for the replies. Aarnt - I tried your method and first tried to just get a room attribute written when a room is created. The module compiles error free and loads into Union ok, but doesn't do what I would expect it to do. Can you spot where it might be going wrong, or perhaps if you could put your small example together for me that may be better. Here's the code I'm using:

Code: Select all
package net.user1.union.servermodule;

import net.user1.union.api.Module;
import net.user1.union.core.context.ModuleContext;
import net.user1.union.api.Room;
import net.user1.union.core.attribute.Attribute;
import net.user1.union.core.event.RoomEvent;
import net.user1.union.api.Client;
import net.user1.union.api.Server;
import net.user1.union.core.event.ServerEvent;
import net.user1.union.core.exception.AttributeException;

public class LogoutHandlerServerModule implements Module, Runnable {
   private ModuleContext m_ctx;
   private Thread m_thrThis;
   
   public boolean init(ModuleContext ctx) {
      m_ctx = ctx;
      
        m_thrThis = new Thread(this);
        m_thrThis.start();

      return true;
   }
   
   public void run(){
      Server server = m_ctx.getServer();
      server.addEventListener(ServerEvent.ROOM_CREATED, this, "onRoomCreated");
   }
   
   public void onRoomCreated(ServerEvent evt){
      Room room = evt.getRoom();
      try{
         room.setAttribute("serverModuleAttribute", "The value", null, Attribute.FLAG_SHARED|Attribute.FLAG_PERSISTENT);
      }catch(AttributeException e){
         //
      }
   }
   
   public void shutdown() {
      m_thrThis = null;
   }
}


Thanks for your help,
-Steve
stevemann
 
Posts: 20
Joined: Sat 26 May 2012 02:32

Re: Handling clients leaving rooms

Postby stevemann » Wed 10 Oct 2012 04:44

Fixed it - the problem was in the line which sets the room attribute. I had:
Code: Select all
room.setAttribute("serverModuleAttribute", "The value", null, Attribute.FLAG_SHARED|Attribute.FLAG_PERSISTENT);

The scope cannot be null - it must be global, so the line should be:
Code: Select all
room.setAttribute("serverModuleAttribute", "The value", Attribute.SCOPE_GLOBAL, Attribute.FLAG_SHARED|Attribute.FLAG_PERSISTENT);
stevemann
 
Posts: 20
Joined: Sat 26 May 2012 02:32

Re: Handling clients leaving rooms

Postby toby » Fri 19 Oct 2012 00:36

> The rooms in the system are all created dynamically which excludes the possibility of writing room modules -


Here is how to do that:

I have an extra room (called Lobby, which also serves other purposes),
and instead of clients creating rooms direcly via Create Room calls,
I have them send a module message to Lobby requesting it be done there.

You could probably also do it with a Server Module, but I found Room Modules
easier to work with.

It might also be possible to do all of this on the Client Side, but I wanted to
have more control of it, so I do it on the Server side.

In the Lobby Module (which is loaded from Union.xml), the code is as follows,
using Javascript modules:

When you receive a "new room" request message, do this:

Code: Select all
var roomDef = new RoomDef();
roomDef.setRoomID(nameOfRoom);
var md = new ModuleDef();
md.setType(ModuleDef.SCRIPT);
md.setSource("ModuleCode.js");
roomDef.addModule(md);
server.createRoom(roomDef);


That allows dynamically created rooms, but with static module code.
If you want that to be dynamic too, you'll have to figure out how to get it into
the modules directory prior to executing the above.
toby
 
Posts: 162
Joined: Sun 24 Jun 2012 14:18

Re: Handling clients leaving rooms

Postby stevemann » Fri 19 Oct 2012 02:20

Thanks very much for the input Toby - that's a great way to handle the issue and one I hadn't thought of. I seem to be having success now using the server module suggestion from Aarnt, so personally will carry on with that method. However I think given the choice at the beginning I would have followed yours - if only because I am much more comfortable working with JavaScript than Java.

Thanks for your time.

-Steve
stevemann
 
Posts: 20
Joined: Sat 26 May 2012 02:32

Re: Handling clients leaving rooms

Postby derek » Fri 16 Nov 2012 02:07

Some great ideas in this thread.

I wanted to add that if you do want to dynamically attach a module to a room before it is created you can do so with the ServerEvent.ROOM_CREATE_REQUESTED event. The event contains the RoomDef that will be used to create the room. You can use the RoomDef to add a room module.

For example:

Code: Select all
public void onRoomCreateRequested(ServerEvent evt) {
        // could also add a condition to only add to rooms with a particular qualifier or room ID
        RoomDef roomDef = (RoomDef)evt.getProperty(ServerEvent.PROP_ROOMDEF);
        ModuleDef moduleDef = new ModuleDef();
        moduleDef.setType(ModuleDef.CLASS);
        moduleDef.setSource("com.example.ClientManagementRoomModule");
        roomDef.addModule(moduleDef);
}


Cheers,

Derek
derek
 
Posts: 68
Joined: Mon 17 Oct 2011 19:12


Return to Union Platform

Online

Users browsing this forum: No registered users and 1 guest