在做游戏跨服的时候,需要试用smartfoxserver来连接smartfoxserver,所以需要smartfoxserver做客户端
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import it.gotoandplay.smartfoxclient.*;
import it.gotoandplay.smartfoxserver.crypto.MD5;
import it.gotoandplay.smartfoxserver.db.DataRow;
import it.gotoandplay.smartfoxserver.db.DbManager;
public class LoginClient implements ISFSEventListener
{
SmartFoxClient sfc;
/**main函数主入口
* @param args
*/
public static void main(String[] args)
{
// 创建接口
sfc = new SmartFoxClient(true);
// 实例化所要连接的拓展类
LoginClient login = new LoginClient();
// 添加事件处理程序(知列举了其中三个事件,还有很多事件)
sfc.addEventListener(SFSEvent.onConnection, (ISFSEventListener) login); // 处理连接
sfc.addEventListener(SFSEvent.onLogin, (ISFSEventListener) login);// 登陆
sfc.addEventListener(SFSEvent.onExtensionResponse,(ISFSEventListener) login);// 从服务器派遣任务
// 连接到服务器(这里是本机)
sfc.connect("127.0.0.1",9339);
}
/*
* 分别处理sfs事件
*/
public void handleEvent(SFSEvent evt)
{
// 接受任何字符的处理
JSONObject json = new JSONObject();
//处理连接服务器smartfoxserver事件
if (evt.getName().equals(SFSEvent.onConnection))
{
//LoginExtension代表连接的拓展名,caoyanhua代表连接的用户名,第三个参数为密码,此处为空
sfc.login("LoginExtension", "caoyanhua", "");
//使用JSONObject传送消息
JSONObject send = new JSONObject();
try {
send.put("cmd", "login");
send.put("username", "caoyanhua");
send.put("pwd", "123456");
} catch (JSONException e) {
e.printStackTrace();
}
//自动加入服务端的autojoin房间
sfc.autoJoin();
//发送消息到服务端
sfc.sendXtMessage("LoginExtension", "cmd", send, 1);
}
else if (evt.getName().equals(SFSEvent.onLogin))
{//处理用户登陆事件
sfc.getRoomList();// 得到房间的列表
JSONObject send = new JSONObject();
try {
send.put("cmd", "login");
send.put("username", "caoyanhua");
send.put("pwd", "123456");
} catch (JSONException e) {
e.printStackTrace();
}
sfc.sendXtMessage("LoginExtension", "cmd", send, 1);
}
else if (evt.getName().equals(SFSEvent.onExtensionResponse))
{//监听服务端拓展传递给客户端的数据
//打印传递过来的全部数据
Object _obj1 = evt.getParams().get("dataObj");
System.out.println("allData:" + evt.getParams().get("dataObj"));
//传递的list数据
System.out.println("list" + evt.getParams().getObj("llst"));
// System.out.println(evt.getParams().get("type"));
// System.out.println("getlist = " + evt.getParams());
}
}
}