Page 1 of 1

Java extension and USER_DISCONNECT event help

Posted: 29 May 2012, 15:26
by psych
Hi,

I am trying to create extension that will detect USER_DISCONNECT event and dispatch notice to AS 3.0 client side with the name of the user who disconnected. Here what i have so far:

Code: Select all

package sfs2x.extension.userdisconnect.src;


import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class UserDisconnectExtension extends SFSExtension
{
       @Override
       public void init()
       {
               trace("Initialising SmartFox Extension example");

               addEventHandler(SFSEventType.USER_DISCONNECT, UserDisconnectHandler.class);
               addEventHandler(SFSEventType.USER_LOGOUT, UserDisconnectHandler.class);
               addEventHandler(SFSEventType.USER_LEAVE_ROOM, UserDisconnectHandler.class);
       }

       @Override
       public void destroy()
       {
               super.destroy();
               removeRequestHandler("example_request");
               removeEventHandler(SFSEventType.USER_DISCONNECT);
       }
}

Code: Select all

package sfs2x.extension.userdisconnect.src;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class UserDisconnectHandler extends BaseServerEventHandler
{
       @Override
       public void handleServerEvent(ISFSEvent arg0) throws SFSException
       {
               User user = (User)arg0.getParameter(SFSEventParam.USER);
               trace("Event Received. User has disconnected: " + user.getName());

               //Same string as per SmartFoxClient's onExtensionResponse() function.
               //Returns some mock data to the client
               send("server_return_data", getReturnData(), user);
       }

       public ISFSObject getReturnData()
       {
               ISFSObject returnData = SFSObject.newInstance();
               returnData.putInt("foo", 123);
               return returnData;
       }

}
And in AS 3.0 i have a listener:

Code: Select all

sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);

private function onExtensionResponse(event:SFSEvent):void
		{
			MonsterDebugger.trace(this, "[SmartFoxConnector] Got an extension response!");
			var params : ISFSObject = event.params.params;
			var cmd : String = event.params.cmd;
			
			switch(cmd)
			{
				//Refer to the java UserJoinedRoomHandler class to see where
				//this string is sent from
				case "server_return_data":
				                                       MonsterDebugger.trace(this, "Returned value of params.foo: " + params.getInt("foo"));
								       break;
			}
		}
But what happens is that when i run sfs2x-standalone console i see the first trace:
"Initialising SmartFox Extension example"
But when i close a flash player window with game instance running in it no event gets received by client side. Can anyone please help me, what am i doing wrong here?

Thanks in advance,
Best regards

Re: Java extension and USER_DISCONNECT event help

Posted: 29 May 2012, 22:18
by 3slices
I don“t know what you are trying to achieve. But for me it looks like you are trying to send an extension response to the user that has already disconnected. This cannot work.

Re: Java extension and USER_DISCONNECT event help

Posted: 30 May 2012, 07:44
by psych
Hi,

What i am trying to achieve is to send notification to all connected users when a user disconnects. Perhaps i am doing this the wrong way, can you point me in a right direction? How can i achieve that?

Thanks in advance,
Best regards

Re: Java extension and USER_DISCONNECT event help

Posted: 30 May 2012, 08:47
by psych
I got the extension part working in a way that extension now detects user disconnect and traces out the message from event handler in Java. But all connected Flash clients do not recieve:

Code: Select all

 send("server_return_data", getReturnData(), user);
My guess is that the above line sends the return data to just disconnected client instead to all of them, am I right? How can i send the return param getReturnData() to all sfs connected flash clients?

Thanks,
Cheers

Re: Java extension and USER_DISCONNECT event help

Posted: 30 May 2012, 10:44
by 3slices
Your variable user seems to contain only the user who has been disconnected. Therfor the response gets sent back to him. So you need to build the list of all users who should receive that message.
Also you should use the SFS api to send the extension response http://docs2x.smartfoxserver.com/api-do ... onResponse(java.lang.String, com.smartfoxserver.v2.entities.data.ISFSObject, java.util.List, com.smartfoxserver.v2.entities.Room, boolean)

Re: Java extension and USER_DISCONNECT event help

Posted: 30 May 2012, 10:54
by foxboy
Dude,

As much as possible, do not re-invent the wheel :)

I think what you are trying to achieve is already being done by the Room logic. If you want data shared by users, then just make a Room Variable available for them so that each one can have access to the shared variable by just listening for the events on your client side. What you can do for example is on your UserDisconnectExtension() event handler, you can still update the Room Variable via the last joined room method and then set some values on the Room Variable to indicate that one player was disconnected from the room. That way, all other connected users to the room will receive the event. Just make sure you fire the update variable event of course since you are changing the room variable on the server side. On the client side, updating a room variable automatically updates the other clients.