SmartFoxServer学习(4)--登陆

前期准备工做前面都已作好, 就不在多说, 直接功能实现了!session

 

SmartFoxServer的登陆分Guest LoginCustom Login 两种, 默认为Guset Loginide

Guset Login加密

  1. 只须要用户名, 甚至用户名都不须要, 密码就更不须要了, 直接发送LoginRequest 便可, 不作任何验证spa

  2. 不会触发服务端的USER_LOGIN 事件3d

Gustom Logincode

  1. 须要用户名, 须要密码, 验证经过后才算登陆成功blog

  2. 会触发服务端的USER_LOGIN 事件事件

 

Custom Login 开启方式get

登陆后台, 进入 Zone Configurator, 选中要设置的Zone, 双击进入编辑模式, 在General tab页找到Use Custom Login, 将其设置为开启状态.input

 

下面是Custom Login实现 

客户端实现

1. 添加监听事件

1 //add events
2 _sfs.AddEventListener(SFSEvent.CONNECTION, onConnection);
3 _sfs.AddEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost);
4 
5 _sfs.AddEventListener(SFSEvent.LOGIN, onLogin);
6 _sfs.AddEventListener(SFSEvent.LOGIN_ERROR, onLoginError);
View Code

2. 链接成功后发送登陆请求

1 ISFSObject paras = SFSObject.NewInstance();
2 paras.PutUtfString("test", "登陆参数");
3 // 参数1: 用户名
4 // 参数2: 密码
5 // 参数3: 登入的Zone名称
6 // 参数4: 额外参数
7 IRequest req = new LoginRequest(inputUsername.text, inputPassword.text, "GameZone", paras);
8 _sff.Send(req);
View Code

3. 若登陆成功会触发LOGIN 的监听方法, 失败则会触发LOGIN_ERROR 的监听方法.

 

服务端实现

1. 添加USER_LOGIN事件处理类

1 addEventHandler(SFSEventType.USER_LOGIN, OnUserLogin.class);
View Code
1 public class OnUserLogin extends BaseServerEventHandler {
2 
3     @Override
4     public void handleServerEvent(ISFSEvent event) throws SFSException {
5                 // do something
6     }
7 }
View Code

2. 登陆验证

 1 @Override
 2 public void execute(ISFSEvent event) throws SFSLoginException {
 3     
 4     // 用户名
 5     String username = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
 6     // 密码(密文)
 7     String password = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
 8     // 当前Session
 9     Session session = (Session) event.getParameter(SFSEventParam.SESSION);
10     
11     // 密码(明文),与客户端密码进行验证
12     String pwd = "test";
13     if (!getApi().checkSecurePassword(session, pwd, password)) {
14         // 验证失败,抛出登陆异常信息
15         throw new SFSLoginException("密码错误!");
16     }else{
17         // 验证经过
18     }
19 }
View Code

 

须要注意的地方

1. 客户端的密码是通过加密后发送的, 非明文!

2. 服务端若是验证失败直接抛出异常便可, SmartFoxServer会本身封装请求返回, 本身不用另外推送消息.

 

整个登陆过程仍是挺简单的, 具体流程为, 首先客户端和服务端分别监听本身的登陆事件, 当链接成功后客户端发送登陆请求, 服务端触发USER_LOGIN事件进行验证, 验证难过客户端触发LOGIN事件, 验证失败抛出SFSLoginException, 客户端触发LOGIN_ERROR事件.

 

本文版权归做者和博客园共有,来源网址:http://www.cnblogs.com/code-boy/欢迎各位转载,可是未经做者本人赞成,转载文章以后必须在文章页面明显位置给出做者和原文链接,不然保留追究法律责任的权利。

相关文章
相关标签/搜索