纳金网
标题:
SmartFoxServer 连接服务端
[打印本页]
作者:
王者再临
时间:
2014-12-31 22:07
标题:
SmartFoxServer 连接服务端
using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities.Data;
using Sfs2X.Requests;
using Sfs2X.Entities;
public class SFS2 : MonoBehaviour {
private bool isLogionIn;
private bool isconect;
private bool isZone;
#region 初始化网络成员变量 定义
public string ServerIP = "127.0.0.1";
public int ServerPort = 9933;
public string UserName = "12454";
public string Password = "454545";
private string ZoneName = "exmple";
SmartFox sfs;
#endregion
// Use this for initialization
void Start()
{
sfs = new SmartFox();
sfs.ThreadSafeMode = true;
sfs.AddEventListener(SFSEvent.CONNECTION, onConnect);
sfs.AddEventListener(SFSEvent.LOGIN, onLoginSuccess);
sfs.AddEventListener(SFSEvent.LOGIN_ERROR, onLoginError);
sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
sfs.AddEventListener(SFSEvent.ROOM_ADD, OnRoomAdd);//添加房间
sfs.AddEventListener(SFSEvent.ROOM_REMOVE, OnRoomMove);
sfs.AddEventListener(SFSEvent.USER_ENTER_ROOM, OnUserEntryRoom);
sfs.AddEventListener(SFSEvent.USER_EXIT_ROOM, OnUserExitRoom);
//sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse)
sfs.Connect(ServerIP, ServerPort);
SFSObject sfsobj = new SFSObject();
string shebei = Sfs2X.Util.PasswordUtil.MD5Password(SystemInfo.deviceUniqueIdentifier)
}
void onLoginSuccess(BaseEvent e)
{
Debug.Log("login success!" + e.Params["user"]);
isLogionIn = true;
isZone = true;
ISFSObject obj = new SFSObject();
obj.PutInt("NumA", 2);
obj.PutInt("NumB", 5);
sfs.Send(new ExtensionRequest("SumNumbers", obj));
}
void onLoginError(BaseEvent e)
{
Debug.Log("login success");
}
void OnLoginOut(BaseEvent e)
{
Debug.Log("exit success!");
isLogionIn = false;
isZone = false;
}
void onExtensionResponse(BaseEvent e)
{
string cmd = (string)e.Params["cmd"];
ISFSObject objIn = (SFSObject)e.Params["params"];
if (cmd == "uir")
{
if (objIn.ContainsKey("rs"))
{
Debug.Log(objIn.GetByte("rs"));
Debug.Log("userid: " + objIn.GetInt("ui"));
Debug.Log("userName: " + objIn.GetUtfString("un"));
Debug.Log("gold: " + objIn.GetLong("gd"));
}
}
if (cmd == "bdr")
{
if (objIn.ContainsKey("rs"))
{
Debug.Log(objIn.GetByte("rs"));
Debug.Log("BODYlist: " + objIn.GetSFSArray("bl"));
}
}
}
void onConnect(BaseEvent e)
{
if ((bool)e.Params["success"])
{
Debug.Log("Successfly Connect!");
isconect = true;
//sfs.Send(new LoginRequest(UserName, "", ZoneName));
}
else
{
Debug.Log("connet fill!");
}
}
void OnConntctionError(BaseEvent e)
{
isconect = false;
Debug.Log("程序与服务器断开!原因是:"+(string)e.Params["reason"]);
}
// Update is called once per frame
void Update()
{
sfs.ProcessEvents();
}
void OnJoinRoom(BaseEvent e)
{
Debug.Log("insert room" );
// sfs.Send(new PublicMessageRequest("hello world!"))
}
void OnJoinRoomError(BaseEvent evt)
{
Debug.Log("OnJoinRoomError");
}
void OnRoomMove(BaseEvent evt)
{
Debug.Log("OnRoomMove");
}
void OnRoomAdd(BaseEvent evt)
{
Debug.Log("房间添加成功");
}
void OnUserEntryRoom(BaseEvent evt)
{
Debug.Log("OnUserEntryRoom");
}
void OnUserExitRoom(BaseEvent evt)
{
Debug.Log("OnUserExitRoom");
}
void OnPublicMessage(BaseEvent e)
{
Room room=(Room)e.Params["room"];
User sender=(User)e.Params["sender"];
Debug.Log("["+room.Name+"]"+sender.Name+":"+e.Params["message"]);
}
void OnGUI()
{
SFSObject sfsobj = new SFSObject();
if (GUI.Button(new Rect(100, 100, 100, 100), "connet server"))
{
sfsobj.PutUtfString("mi", "111111");
UserName = UserName + Random.RandomRange(100, 999).ToString();
byte[] testByteArray = System.Text.Encoding.UTF8.GetBytes(UserName);
UserName = System.Text.Encoding.UTF8.GetString(testByteArray);
byte[] testByteArray1 = System.Text.Encoding.UTF8.GetBytes(Password);
Password = System.Text.Encoding.UTF8.GetString(testByteArray);
sfs.Send(new LoginRequest(UserName, Password, ZoneName, sfsobj))
} if (isconect && isLogionIn)
{
sfs.Disconnect()
}
else
{
sfs.Connect(ServerIP, ServerPort)
} if (isLogionIn)
{
sfsobj.PutByte("rc",1);
sfs.Send(new ExtensionRequest("uir",sfsobj));
}
}
}
复制代码
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5