Client listener not firing for events sent by Extension

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

hitesh.rawtani
Posts: 4
Joined: 08 Jul 2011, 04:22

Client listener not firing for events sent by Extension

Postby hitesh.rawtani » 10 Jul 2011, 05:58

Hi

I am unable to get the client listeners to fire for events sent by the extension. For SFS events such as LOGIN, ROOM_JOIN, USER_ENTER_ROOM, they're firing alright, but for events sent by my Extension such as "START", "MOVE", they're not. I come to know from the trace on the server side that the code is being executed. But the client side handler method does not fire.

Server Extension Code as follows:

Code: Select all

public class MyExtension extends SFSExtension {
   @Override
   public void init() {
      trace(" MyExtension.init() - Registering Request Handler");
      addRequestHandler("START", StartRequestHandler.class);
   }
}

public class StartRequestHandler extends BaseClientRequestHandler {
   @Override
   public void handleClientRequest(User sender, ISFSObject param) {
      int round = param.getInt("round");
      trace(sender.getName() + " has asked to start round " + round);
      ISFSObject resObj = new SFSObject();
      resObj.putInt("round",round);
      send("START",resObj,sender);
   }
}

Client code added to the dispatch method of SFSEventHandler in BasicClient example of standard Java Client released as follows:

Code: Select all

public void dispatch(BaseEvent event) throws SFSException
{
   System.out.println("Event.getType:" + event.getType());
   .
   .
   else if (event.getType().equals(SFSEvent.ROOM_JOIN))
   {
          System.out.println("Room joined: " + event.getArguments().get("room"));
          System.out.println("Now asking server to start");
          ISFSObject obj = new SFSObject();
          obj.putInt("round", 1);
          sfs.send(new ExtensionRequest("START",obj));
   }   
   else if (event.getType().equals("START"))
   {
          int round = (Integer)event.getArguments().get("round");
          System.out.println("Server accepts your request to start round " + round);
   }
}

The client output is:
Event.getType:connection
Connected. Now sending login request.
Event.getType:login
Logged in. Now joining 'The Lobby' room
Event.getType:roomJoin
Room joined: [Room: The Lobby, Id: 2, GroupId: default]
Now asking server to start

The server output is:
Guest#3 has asked to start round 1

The standard java client example released has only SFS Events, though I am sure Responses to Client Requests and Extension sent events work in a similar fashion. I have surely missed out something elementary. Are there any configuration changes I have to do, do these events have to be registered on the admin portal as zone/room variables?

Can some one please tell me what am I missing? I have been stuck on it for a couple of days.

Thanks.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 10 Jul 2011, 07:40

Sure thing.

Extension responses come into a single callback that is not used in the form you do it.

Check the documentation example:
http://docs2x.smartfoxserver.com/api-do ... N_RESPONSE

You sign up to the generic extension response - and then need to have some "response type" variable you can query. I usually use a "cmd" variable to put START, MOVE or similar into it

Hope that makes sense

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
hitesh.rawtani
Posts: 4
Joined: 08 Jul 2011, 04:22

that solved it

Postby hitesh.rawtani » 10 Jul 2011, 18:56

Thanks Thomas

It makes complete sense to me now. However I did put anything in "cmd" variable on server side. I directly called send("start",sfsObj,sender) and SFS put the "start" against "cmd" in the event arguments map.

Thanks for the quick reply. Appreciate it.
aperfectsquare
Posts: 2
Joined: 28 Jul 2011, 22:37

Postby aperfectsquare » 28 Jul 2011, 22:46

Code: Select all

 sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, new IEventListener() {
 public void dispatch(BaseEvent evt) throws SFSException {
 if (evt.getArguments().get("cmd") == "add") {
 ISFSObject responseParams = (SFSObject)evt.getArguments().get("params");


SFSObject & ISFSObject are not implemented in the java client api do you all import from the server api, or i miss something?
User avatar
Bax
Site Admin
Posts: 4610
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Postby Bax » 29 Jul 2011, 07:30

@aperfectsquare, import from the serve-side API.
Paolo Bax
The SmartFoxServer Team

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 22 guests