为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

SmartFoxServer服务器端用户登录验证

2012-01-05 11页 pdf 73KB 52阅读

用户头像

is_605970

暂无简介

举报
SmartFoxServer服务器端用户登录验证   说明:本示例演示的是一个 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....
SmartFoxServer服务器端用户登录验证
  说明:本示例演示的是一个 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.smartfoxserver.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.smartfoxserver.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 = 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 fromRoom) { // 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"); //ScocketChannel 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.getUserId())); 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.helper.sendRoomList(chan); } } } 其中的一些不明白的地方,大家到网上自己查询。 二,将写好的MyExtension.java编译成MyExtension.class文件。我的 SmartFoxServer安装在 D盘下。所以将编 译好的文件拷贝到 D:\Program Files\SmartFoxServerPRO_1.6.6\Server\javaExtensions\com\song\unity3d \login文 件夹下. 三,打开 D:\Program Files\SmartFoxServerPRO_1.6.6\Server目录下的 config.xml 文件。将如下代码拷贝 到区间。保存后重启 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 请输入相同的用户名和密码,服务器端扩展做的是用户名=密码 的验证简单验证 * */ 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 password="";//密码 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.runInBackground = 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; SFSEvent.onLogin += OnLogin; SFSEvent.onRoomListUpdate += OnRoomListUpdate; SFSEvent.onDebugMessage += OnDebugMessage;     SFSEvent.onExtensionResponse += OnExtensionResponse;//注册一个 SFSEvent事 件 } void FixedUpdate() { smartFox.ProcessEventQueue(); } private void UnregisterSFSSceneCallBacks(){ SFSEvent.onConnection-=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.Connection=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,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.username, 25); GUI.Label(new Rect(10,156,100,100),"Password: "); password=GUI.PasswordField(new Rect(100,156,200,20),this.password,"*"[0],25); GUI.Label(new Rect(10, 190, 100, 100), errorMessage); if ( GUI.Button(new Rect(100, 230, 100, 24), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) { smartFox.Login(zone, username, password); } } else {     GUI.Label(new Rect(10, 150, 100, 100), "Waiting for connection"); GUI.Label(new Rect(10, 190, 100, 100), errorMessage); } } void OnLogin(bool success, string name, string error){ if(success){ Debug.Log("Hello:Welcome "+name); }else{ errorMessage=error; } } void OnRoomListUpdate(Hashtable roomList){ UnregisterSFSSceneCallBacks(); Application.LoadLevel("game"); } /** * 从服务器端扩展接收的信息处理 * */ public void OnExtensionResponse(object data, string type) {       // We only use XML based messages in this tutorial, so ignore string and json types if ( type == SmartFoxClient.XTMSG_TYPE_XML ) { // For XML based communication the data object is a SFSObject SFSObject dataObject = (SFSObject)data; switch(dataObject.GetString("cmd")){ case "loginOK": //说明用户登录成功 Debug.Log("You pass validation!"); smartFox.myUserName = dataObject.GetString("name");//服务器端传递的用户名 smartFox.myUserId = int.Parse(dataObject.GetString("id"));//服务器端传递的 ID break; case "loginKO": //登录失败,在这可以做相应的处理,如跳转到注册的页面。。。 errorMessage="You not pass validation!"; break; } } } }
/
本文档为【SmartFoxServer服务器端用户登录验证】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索