SmartFoxServer服务器端用户登录验证

上传人:ZJ****4 文档编号:46802213 上传时间:2018-06-28 格式:PDF 页数:11 大小:73.82KB
返回 下载 相关 举报
SmartFoxServer服务器端用户登录验证_第1页
第1页 / 共11页
SmartFoxServer服务器端用户登录验证_第2页
第2页 / 共11页
SmartFoxServer服务器端用户登录验证_第3页
第3页 / 共11页
SmartFoxServer服务器端用户登录验证_第4页
第4页 / 共11页
SmartFoxServer服务器端用户登录验证_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《SmartFoxServer服务器端用户登录验证》由会员分享,可在线阅读,更多相关《SmartFoxServer服务器端用户登录验证(11页珍藏版)》请在金锄头文库上搜索。

1、 说明:本示例演示的是一个 SmartFoxServer 服务器端用户登录验证。 开发环境:Unity3d 2.6,SmartFoxServer 1.6.6,MyEclipse 一,首先先写一个服务器端扩展的 Java 类。如下所示 package com.song.unity3d.login; import java.nio.channels.SocketChannel; import java.util.LinkedList; import it.gotoandplay.smartfoxserver.data.User; import it.gotoandplay.smartfoxserv

2、er.data.Zone; import it.gotoandplay.smartfoxserver.events.InternalEventObject; import it.gotoandplay.smartfoxserver.exceptions.LoginException; import it.gotoandplay.smartfoxserver.extensions.AbstractExtension; import it.gotoandplay.smartfoxserver.extensions.ExtensionHelper; import it.gotoandplay.sma

3、rtfoxserver.lib.ActionscriptObject; /* * * author songvery * version 2011-03-09-01 * */ public class MyExtension extends AbstractExtension private ExtensionHelper helper; private Zone currentZone; /private DbManager db; LinkedList recipients; public void init() recipients=new LinkedList(); helper =

4、ExtensionHelper.instance(); this.currentZone = helper.getZone(this.getOwnerZone(); public void destory() public void handleRequest(String cmd, ActionscriptObject ao, User user, int fromRoom) / TODO Auto-generated method stub public void handleRequest(String cmd, String params, User user, int fromRoo

5、m) / TODO Auto-generated method stub public void handleInternalEvent(InternalEventObject ie) if(ie.getEventName().equals(“loginRequest“) ActionscriptObject response=new ActionscriptObject(); User loginUser=null; /用户名 String name=ie.getParam(“nick“); /密码 String pwd=ie.getParam(“pass“); /ScocketChanne

6、l SocketChannel chan=(SocketChannel)ie.getObject(“chan“); /String serverRandom=helper.getSecretKey(chan); /省去查询数据库验证的操作,自己写吧! if(name.equals(pwd) try loginUser=helper.canLogin(name, pwd, chan, this.currentZone.getName(); response.put(“cmd“, “loginOK“); response.put(“id“, String.valueOf(loginUser.get

7、UserId(); response.put(“name“, loginUser.getName(); catch(LoginException e) response.put(“cmd“, “loginKO“); response.put(“err“, e.getMessage(); else response.put(“cmd“, “loginKO“); response.put(“err“, “error password“); recipients.add(chan); this.sendResponse(response, -1, null, recipients); this.he

8、lper.sendRoomList(chan); 其中的一些不明白的地方,大家到网上自己查询。 二,将写好的 MyExtension.java 编译成 MyExtension.class 文件。我的 SmartFoxServer 安装在 D 盘下。所以将编 译好的文件拷贝到 D:Program FilesSmartFoxServerPRO_1.6.6ServerjavaExtensionscomsongunity3d login 文 件夹下. 三,打开 D:Program FilesSmartFoxServerPRO_1.6.6Server 目录下的 config.xml 文件。 将如下代码拷

9、贝 到区间。 保存后重启 SmartFoxServer 服 务器. 四,在 Unity3d 中创建一个 C#文件。代码如下所示. using UnityEngine; using System; using System.Collections; using SmartFoxClientAPI; using SmartFoxClientAPI.Util; using SmartFoxClientAPI.Data; /* * author songvery * version 2011-03-09-01 * infomation 请输入相同的用户名和密码,服务器端扩展做的是用户名=密码 的验证简

10、单验证 * */ public class ConnectionGUI: MonoBehaviour private SmartFoxClient smartFox; private bool shuttingDown = false; private string serverName = “127.0.0.1“;/服务器 IP private int serverPort = 9339;/服务器端口号 private string zone = “firstZone“; private string username=“songvery“;/用户名 private string passw

11、ord=“;/密码 private string errorMessage = “;/错误信息 private string extensionName=“;/服务器端扩展名 /* * 构造函数 * */ public ConnectionGUI() extensionName=“firstSFS“; void OnApplicationQuit() shuttingDown = true; private bool connectionAttempt = false; / Use this for initialization void Start () Application.runInB

12、ackground = true; bool debug=true; if(SmartFox.IsInitialized() smartFox=SmartFox.Connection; else try smartFox=new SmartFoxClient(debug); smartFox.runInQueueMode = true; catch(Exception e) errorMessage=e.ToString(); SFSEvent.onConnection += OnConnection; SFSEvent.onConnectionLost += OnConnectionLost

13、; SFSEvent.onLogin += OnLogin; SFSEvent.onRoomListUpdate += OnRoomListUpdate; SFSEvent.onDebugMessage += OnDebugMessage; SFSEvent.onExtensionResponse += OnExtensionResponse;/注册一个 SFSEvent 事 件 void FixedUpdate() smartFox.ProcessEventQueue(); private void UnregisterSFSSceneCallBacks() SFSEvent.onConne

14、ction-=OnConnection; SFSEvent.onConnectionLost-=OnConnectionLost; SFSEvent.onLogin-=OnLogin; SFSEvent.onRoomListUpdate-=OnRoomListUpdate; SFSEvent.onDebugMessage-=OnDebugMessage; SFSEvent.onExtensionResponse -= OnExtensionResponse; void OnConnection(bool success,string error) if(success) SmartFox.Co

15、nnection=smartFox; else errorMessage=error; void OnConnectionLost() Debug.Log(“OnConnectionLost“); errorMessage=“connection lost/No connnection to server“; void OnDebugMessage(string message) Debug.Log(“SFSDEBUG -“+message); /* * 尝试与服务器的连接 * */ void ConnectToServer() try smartFox.Connect(serverName,

16、serverPort); catch(Exception e) errorMessage=e.ToString(); /Application.ExternalEval (“history.go(-1);“); void OnGUI() if (!connectionAttempt) connectionAttempt = true; ConnectToServer(); else if (smartFox.IsConnected() / Login GUI.Label(new Rect(10, 116, 100, 100), “Username: “); username = GUI.TextField(new Rect(100, 116, 200, 20), this.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号