I am new to smartfox server and have been trying some basic codes how to implement it..This my code..I have been trying the basic addition of two nos. response from server code.. This is my android code...
sfs=new SmartFox();
// tv.setText("Disconnected");
sfs.addEventListener(SFSEvent.LOGIN, new IEventListener() {
@Override
public void dispatch(BaseEvent evt) throws SFSException {
// TODO Auto-generated method stub
String parms = evt.getArguments().toString();
Log.v("Args", parms);
if(evt.getType().equalsIgnoreCase(SFSEvent.LOGIN))
{
sfs.send(new JoinRoomRequest("Demo Lobby"));
Log.d("log9", "after sending req to join room");
}
else
{
Log.d("log8", "Joining not allowed");
}
}
});
sfs.addEventListener(SFSEvent.ROOM_JOIN, new IEventListener() {
@Override
public void dispatch(BaseEvent evt) throws SFSException {
// TODO Auto-generated method stub
String par=evt.getArguments().toString();
Log.d("rooms", par);
SFSObject sfsobj=new SFSObject();
sfsobj.putInt("n1", 11);
sfsobj.putInt("n2", 13);
sfs.send(new ExtensionRequest("add",sfsobj));
Log.d("log10", "request sent");
}
});
sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, new IEventListener() {
@Override
public void dispatch(BaseEvent evt) throws SFSException {
// TODO Auto-generated method stub
Log.d("log11", "Into Extension Response");
// Map params=arg0.getArguments();
// SFSObject sfso=(SFSObject) params.get("add");
// int i=sfso.getInt("sum");
// Log.d("log11", "req accept");
// Log.d("i value", String.valueOf(i));
//SFSObject o=(SFSObject) arg0.getArguments().get("add");
//String str=arg0.getArguments().toString();
//Log.d("object recieved", str);
// int i=o.getInt("res");
// Log.d("i value", String.valueOf(i));
// System.out.println(i);
//
if(evt.getArguments().get("cmd")=="add")
{
ISFSObject param=(ISFSObject) evt.getArguments().get("params");
int sum=param.getInt("res");
System.out.println("Summ is:"+sum);
}
}
});
sfs.addEventListener(SFSEvent.CONNECTION, new IEventListener() {
@Override
public void dispatch(BaseEvent evt) throws SFSException {
// TODO Auto-generated method stub
if(evt.getType().equalsIgnoreCase(SFSEvent.CONNECTION))
{
Log.d("log2", "connected"+sfs.getConnectionMode());
//Toast.makeText(ConnectionActivity.this, "Connected", Toast.LENGTH_LONG).show();
System.out.println("Connection success"+sfs.getConnectionMode());
//tv.setText("Status:Connected");
LoginRequest loginRqst = new LoginRequest(userName, password, zoneName);
sfs.send(loginRqst);
}
else
{
Log.d("log3", "connection error"+sfs.getConnectionMode());
//Toast.makeText(ConnectionActivity.this, "Error", Toast.LENGTH_LONG).show();
System.out.println("Connection unsuccess"+sfs.getConnectionMode());
}
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.d("log1", "bfore connect");
sfs.connect(serverIp,port);
sfs.setUseBlueBox(true);
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(sfs.isConnected())
{
sfs.disconnect();
Log.d("log4", "Disconnected");
}
else
{
Log.d("log5", "The connection was never made");
}
}
});
This is my Extension class..
public class MyClassExtension extends SFSExtension {
@Override
public void init() {
// TODO Auto-generated method stub
addRequestHandler("add", ClientServerRequest.class);
}
And this is my handler class..
public class ClientServerRequest extends BaseClientRequestHandler{
@Override
public void handleClientRequest(User arg0, ISFSObject arg1)
{
// TODO Auto-generated method stub
int n1 = arg1.getInt("n1");
int n2 = arg1.getInt("n2");
trace(n1,n2);
// Create a response object
ISFSObject resObj = SFSObject.newInstance();
resObj.putInt("res", n1 + n2);
MyClassExtension ext = (MyClassExtension)getParentExtension();
trace("SUM is "+(n1+n2));
// Send it back
ext.send("add", resObj, arg0);
trace("Extension response has been sent");
}
}
I tried all methods to acquire the result into EXTENSION_RESPONSE..but couldnt get the sum..also in the zone configurator..i have configured it correctly...plz help..thanks in advance..
Let me know something do you got the count in zone when you see in the SFSadmin view, n here you mentioned the zonename does that referes to that zone name and do you have any message in the console [Run SFS with sfs2x.bat rather than the service] any message like client 'xxx' connected ... or no requesthandler registered for "add" sent by client..