查看: 1165|回复: 0
打印 上一主题 下一主题

[其他] SmartFoxServer 连接服务端

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-12-31 22:07:02 |只看该作者 |倒序浏览
  1. using UnityEngine;
  2. using System.Collections;
  3. using Sfs2X;
  4. using Sfs2X.Core;
  5. using Sfs2X.Entities.Data;
  6. using Sfs2X.Requests;
  7. using Sfs2X.Entities;

  8. public class SFS2 : MonoBehaviour {

  9.     private bool isLogionIn;
  10.     private bool isconect;
  11.     private bool isZone;

  12.     #region 初始化网络成员变量 定义
  13.     public string ServerIP = "127.0.0.1";
  14.     public int ServerPort = 9933;
  15.     public string UserName = "12454";
  16.     public string Password = "454545";
  17.     private string ZoneName = "exmple";


  18.     SmartFox sfs;
  19.     #endregion

  20.     // Use this for initialization
  21.     void Start()
  22.     {
  23.         sfs = new SmartFox();
  24.         sfs.ThreadSafeMode = true;

  25.         sfs.AddEventListener(SFSEvent.CONNECTION, onConnect);

  26.         sfs.AddEventListener(SFSEvent.LOGIN, onLoginSuccess);
  27.         sfs.AddEventListener(SFSEvent.LOGIN_ERROR, onLoginError);

  28.         sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
  29.         sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
  30.         sfs.AddEventListener(SFSEvent.ROOM_ADD, OnRoomAdd);//添加房间
  31.         sfs.AddEventListener(SFSEvent.ROOM_REMOVE, OnRoomMove);

  32.         sfs.AddEventListener(SFSEvent.USER_ENTER_ROOM, OnUserEntryRoom);

  33.         sfs.AddEventListener(SFSEvent.USER_EXIT_ROOM, OnUserExitRoom);
  34.         //sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
  35.         sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse)

  36.         sfs.Connect(ServerIP, ServerPort);

  37.         SFSObject sfsobj = new SFSObject();
  38.         string shebei = Sfs2X.Util.PasswordUtil.MD5Password(SystemInfo.deviceUniqueIdentifier)
  39.     }

  40.     void onLoginSuccess(BaseEvent e)
  41.     {
  42.         Debug.Log("login success!" + e.Params["user"]);
  43.         isLogionIn = true;
  44.         isZone = true;

  45.         ISFSObject obj = new SFSObject();
  46.         obj.PutInt("NumA", 2);
  47.         obj.PutInt("NumB", 5);
  48.         sfs.Send(new ExtensionRequest("SumNumbers", obj));

  49.     }

  50.     void onLoginError(BaseEvent e)
  51.     {
  52.         Debug.Log("login success");
  53.     }

  54.     void OnLoginOut(BaseEvent e)
  55.     {
  56.         Debug.Log("exit success!");
  57.         isLogionIn = false;
  58.         isZone = false;
  59.     }

  60.     void onExtensionResponse(BaseEvent e)
  61.     {
  62.         string cmd = (string)e.Params["cmd"];
  63.         ISFSObject objIn = (SFSObject)e.Params["params"];

  64.         if (cmd == "uir")
  65.         {
  66.             if (objIn.ContainsKey("rs"))
  67.             {
  68.                 Debug.Log(objIn.GetByte("rs"));
  69.                 Debug.Log("userid: " + objIn.GetInt("ui"));
  70.                 Debug.Log("userName: " + objIn.GetUtfString("un"));
  71.                 Debug.Log("gold: " + objIn.GetLong("gd"));
  72.             }
  73.         }
  74.         if (cmd == "bdr")
  75.         {
  76.             if (objIn.ContainsKey("rs"))
  77.             {
  78.                 Debug.Log(objIn.GetByte("rs"));
  79.                 Debug.Log("BODYlist: " + objIn.GetSFSArray("bl"));
  80.             }
  81.         }
  82.     }

  83.     void onConnect(BaseEvent e)
  84.     {
  85.         if ((bool)e.Params["success"])
  86.         {
  87.             Debug.Log("Successfly Connect!");
  88.             isconect = true;
  89.             //sfs.Send(new LoginRequest(UserName, "", ZoneName));
  90.         }
  91.         else
  92.         {
  93.             Debug.Log("connet fill!");
  94.         }
  95.     }

  96.     void OnConntctionError(BaseEvent e)
  97.     {
  98.         isconect = false;
  99.         Debug.Log("程序与服务器断开!原因是:"+(string)e.Params["reason"]);
  100.     }
  101.     // Update is called once per frame

  102.     void Update()
  103.     {
  104.         sfs.ProcessEvents();
  105.     }

  106.     void OnJoinRoom(BaseEvent e)
  107.     {
  108.         Debug.Log("insert room" );
  109.        // sfs.Send(new PublicMessageRequest("hello world!"))
  110.     }

  111.     void OnJoinRoomError(BaseEvent evt)
  112.     {
  113.         Debug.Log("OnJoinRoomError");
  114.     }

  115.     void OnRoomMove(BaseEvent evt)
  116.     {
  117.         Debug.Log("OnRoomMove");
  118.     }   
  119. void OnRoomAdd(BaseEvent evt)
  120.     {
  121.         Debug.Log("房间添加成功");
  122.     }

  123.     void OnUserEntryRoom(BaseEvent evt)
  124.     {
  125.         Debug.Log("OnUserEntryRoom");

  126.     }   
  127. void OnUserExitRoom(BaseEvent evt)
  128.     {
  129.         Debug.Log("OnUserExitRoom");        
  130.     }
  131.     void OnPublicMessage(BaseEvent e)
  132.     {
  133.         Room room=(Room)e.Params["room"];
  134.         User sender=(User)e.Params["sender"];
  135.         Debug.Log("["+room.Name+"]"+sender.Name+":"+e.Params["message"]);
  136.     }
  137.     void OnGUI()
  138.     {
  139.         SFSObject sfsobj = new SFSObject();
  140.         if (GUI.Button(new Rect(100, 100, 100, 100), "connet server"))
  141.         {
  142.             sfsobj.PutUtfString("mi", "111111");
  143.             UserName = UserName + Random.RandomRange(100, 999).ToString();
  144.             byte[] testByteArray = System.Text.Encoding.UTF8.GetBytes(UserName);
  145.             UserName = System.Text.Encoding.UTF8.GetString(testByteArray);

  146.             byte[] testByteArray1 = System.Text.Encoding.UTF8.GetBytes(Password);
  147.             Password = System.Text.Encoding.UTF8.GetString(testByteArray);

  148.             sfs.Send(new LoginRequest(UserName, Password, ZoneName, sfsobj))

  149.         }        if (isconect && isLogionIn)
  150.         {
  151.             sfs.Disconnect()
  152.         }
  153.         else
  154.         {
  155.             sfs.Connect(ServerIP, ServerPort)
  156.         }        if (isLogionIn)
  157.         {         
  158.             sfsobj.PutByte("rc",1);
  159.             sfs.Send(new ExtensionRequest("uir",sfsobj));
  160.         }


  161.     }
  162. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-15 01:11 , Processed in 0.214268 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部