这两天熟悉了一下SFS2X的部署开发流程,试着写了一个小例子.笔记一下.
例子的流程是:
1.在admintool里创建zone:sfs2x_demo,将其自定义登陆开启.
2.在admintool里创建Room: station.
3.根据客户端的配置文件sfs-config.xml连接到SFS.
4.自定义登陆,验证用户名和密码的合法性.
5.加入房间,
6.设置用户变量,创建客户端avatar.保存位置到用户变量里去.
7.发扩展,avatar走路通知同房间内的所有用户.
8.监听用户离开事件.清除avatar..
最后,将写好的扩展打成jar包放在SFS2X-RC1\SFS2X\extensions\golastroom\目录下,在admintool里配置一下zone的扩展,注意golastroom的扩展文件名须和AdmonTool配置里zone的扩展名字一样.
这样,每个登陆到此zone的用户都必须通过自定义扩展模块的验证..
服务端java
packagecom.golastroom.extension;
importcom.golastroom.eventHandler.LoginEventHandler;
importcom.golastroom.requestHandler.MoveReqHandler;
importcom.smartfoxserver.v2.annotations.Instantiation;
importcom.smartfoxserver.v2.annotations.Instantiation.InstantiationMode;
importcom.smartfoxserver.v2.core.SFSEventType;
importcom.smartfoxserver.v2.extensions.SFSExtension;
@Instantiation(InstantiationMode.SINGLE_INSTANCE)
publicclassMyFirstExtensionextendsSFSExtension {
@Override
publicvoidinit() {
trace("=============golastroom ==============");
addRequestHandler("move", MoveReqHandler.class);
addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandler.class);
}
@Override
publicvoiddestroy() {
super.destroy();
}
}
扩展里初始化方法里将处理avatar移动的处理模块和自定义登陆模块注册添加进去.
登陆处理模块
packagecom.golastroom.eventHandler;
importcom.smartfoxserver.bitswarm.sessions.ISession;
importcom.smartfoxserver.v2.core.ISFSEvent;
importcom.smartfoxserver.v2.core.SFSEventParam;
importcom.smartfoxserver.v2.entities.data.ISFSObject;
importcom.smartfoxserver.v2.exceptions.SFSException;
importcom.smartfoxserver.v2.exceptions.SFSLoginException;
importcom.smartfoxserver.v2.extensions.BaseServerEventHandler;
publicclassLoginEventHandlerextendsBaseServerEventHandler {
@Override
publicvoidhandleServerEvent(ISFSEvent event)throwsSFSException {
String name =(String)event.getParameter(SFSEventParam.LOGIN_NAME);
String pass = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
ISFSObject paramsOut = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA);
trace(name +" is login .. ");
if(name.startsWith("h_") && getApi().checkSecurePassword(session,"123", pass)){
paramsOut.putBool("login",true);
}else{
paramsOut.putBool("login",false);
//throw new SFSLoginException("用户名必须以\"h_\"开头且密码为:123");
}
}
}
自定义登陆安全验证只是简单验证此用户的用户名必须以h_开头且密码为123为合法用户,返回登陆验证结果.
avatar移动处理模块
packagecom.golastroom.requestHandler;
importcom.smartfoxserver.v2.annotations.Instantiation;
importcom.smartfoxserver.v2.annotations.Instantiation.InstantiationMode;
importcom.smartfoxserver.v2.entities.Room;
importcom.smartfoxserver.v2.entities.User;
importcom.smartfoxserver.v2.entities.data.ISFSObject;
importcom.smartfoxserver.v2.extensions.BaseClientRequestHandler;
@Instantiation(InstantiationMode.SINGLE_INSTANCE)
publicclassMoveReqHandlerextendsBaseClientRequestHandler {
@Override
publicvoidhandleClientRequest(User sender, ISFSObject params) {
trace(sender.getName()," request!! ");
Room room = sender.getLastJoinedRoom();
params.putUtfString("name", sender.getName());
send("p", params, room.getUserList());
}
}
移动处理很简单,只是将用户提交的移动数据转发一下,同时将谁移动了也发给所有用户.