我是 smartfox 服务器的新手,并且一直在尝试一些基本代码如何实现它..这是我的代码..我一直在尝试基本添加两个 no。服务器代码的响应..这是我的android代码...
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");
}
}
});
这是我的扩展课..
public class MyClassExtension extends SFSExtension {
@Override
public void init() {
// TODO Auto-generated method stub
addRequestHandler("add", ClientServerRequest.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");
}
}
我尝试了所有方法将结果获取到 EXTENSION_RESPONSE ..但无法获得总和..也在区域配置器中..我已正确配置它...请帮助..提前谢谢..