SmartFoxServer实现私聊

using UnityEngine;
 
  using System;
 
  using System.Collections;
 
  using System.Collections.Generic;
 
  using System.Runtime.InteropServices;
 
  using System.Security.Permissions;
 
  using System.Text;
 
  using SmartFoxClientAPI;
 
  using SmartFoxClientAPI.Data;
 
  public class LobbyGUI : MonoBehaviour
 
  {  Unity3D教程手册
 
  private SmartFoxClient smartFox;
 
  private string zone = “simpleChat”;
 
  private string username = “”;
 
  private string password = “”;
 
  private string loginErrorMessage = “”;
 
  private static bool isLoggedIn;
 
  private static bool roomListReceived = false;
 
  private static bool F=false;
 
  private static bool ESC=false;
 
  private static bool HY=false;
 
  private static bool windowsKG=false;
 
  private string newMessage = “”;
 
  private ArrayList messages = new ArrayList();
 
  // Locker to use for messages collection to ensure its cross-thread safety
 
  private System.Object messagesLocker = new System.Object();
 
  private Vector2 chatScrollPosition, userScrollPosition;
 
  private int roomSelection = 0;
 
  private string [] roomStrings;
 
  private int userSelection=0;
 
  private string [] userStrings;
 
  public int QJ_userid;
 
  public int QJ_userroomid;
 
  public string QJ_username;
 
  public string QJ_userPrivatemsg;
 
  public GUISkin gSkin;
 
  void Awake ()
 
  {
 
  DontDestroyOnLoad (this);
 
  }
 
  void Start()
 
  {
 
  if (SmartFox.initialized)
 
  {
 
  smartFox = SmartFox.Connection;
 
  }
 
  else
 
  {
 
  Application.LoadLevel(“connect”);
 
  }
 
  // Register callbacks
 
  SFSEvent.onLogin += OnLogin;                      //登录到zone时触发此事件 。
 
  SFSEvent.onLogout += OnLogout;                     //登出成功时触发此事件
 
  SFSEvent.onConnectionLost += OnDisconnect;                 //当 SFS服务器关闭时触发此事件 (从客户端或服务器)。
 
  SFSEvent.onRoomListUpdate += OnRoomList;                 //当接收到当前区域可用房间列表时触发此事件。
 
  SFSEvent.onJoinRoom += OnJoinRoom;                   //成功加入到Room时触发此事件 。
 
  SFSEvent.onPublicMessage += OnPublicMessage;                //当收到一条公有信息时触发此事件 。
 
  SFSEvent.onPrivateMessage+= OnPrivateMessage;               //当收到一条私有信息时触发此事件
 
  SFSEvent.onDebugMessage += OnDebugMessage;                //派发一个调试信息给SmartFoxServer API。
 
  }
 
  void Update () {
 
  if(Input.GetKeyDown(KeyCode.Tab))
 
  {
 
  if(F)
 
  F=false;
 
  else
 
  F=true;
 
  }
 
  if(Input.GetKeyDown(KeyCode.Escape))
 
  {
 
  if(ESC)
 
  ESC=false;
 
  else
 
  ESC=true;
 
  }
 
  }
 
  private void UnregisterSFSSceneCallbacks() {
 
  // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
 
  SFSEvent.onLogin -= OnLogin;
 
  SFSEvent.onLogout -= OnLogout;
 
  SFSEvent.onConnectionLost -= OnDisconnect;
 
  SFSEvent.onRoomListUpdate -= OnRoomList;
 
  SFSEvent.onJoinRoom -= OnJoinRoom;
 
  SFSEvent.onPublicMessage -= OnPublicMessage;
 
  SFSEvent.onPrivateMessage-= OnPrivateMessage;
 
  SFSEvent.onDebugMessage -= OnDebugMessage;
 
  }
 
  // Various SFS callbacks
 
  void OnLogin(bool success, string name, string error)             //登录到zone时触发此事件 。
 
  {
 
  Debug.Log(“On Login callback got: ” + success + “ : ” + error + “ : ” + name);
 
  if (success) {
 
  isLoggedIn = true;
 
  } else {
 
  loginErrorMessage = error;
 
  Debug.Log(“Login error: ”+error);
 
  }
 
  }
 
  void OnLogout()                          //登出成功时触发此事件
 
  {
 
  Debug.Log(“OnLogout”);
 
  isLoggedIn = false;
 
  roomListReceived = false;
 
  }
 
  void OnDisconnect()                        //当 SFS服务器关闭时触发此事件 (从客户端或服务器)。
 
  {
 
  Debug.Log(“OnDisconnect”);
 
  isLoggedIn = false;
 
  roomListReceived = false;
 
  UnregisterSFSSceneCallbacks();
 
  }
 
  public void OnDebugMessage(string message)               //派发一个调试信息给SmartFoxServer API。
 
  {
 
  Debug.Log(“[SFS DEBUG] ” + message);
 
  }
 
  void OnRoomList(Hashtable roomList)                   //当接收到当前区域可用房间列表时触发此事件。
 
  {
 
  try {
 
  List<string> rooms = new List<string>();
 
  foreach (int roomId in roomList.Keys)
 
  {
 
  Room room = (Room)roomList[roomId];
 
  if (room.IsPrivate())
 
  {
 
  continue;
 
  }
 
  Debug.Log(“Room id: ” + roomId + “ has name: ” + room.GetName());
 
  rooms.Add (room.GetName());
 
  }
 
  roomListReceived = true;
 
  roomStrings = rooms.ToArray();                   //房间名数组
 
  // Users always have to be in a room, so lets go to the Hall as our main “lobby”
 
  if (smartFox.GetActiveRoom() == null) {               //返回一个当前活跃的房间对象。
 
  smartFox.JoinRoom(“The Hall”);                  //加入房间
 
  }
 
  }
 
  catch (Exception e) {                       //异常
 
  Debug.Log(“Room list error: ”+e.Message+“ ”+e.StackTrace);
 
  }
 
  }
 
  void OnJoinRoom(Room room)                    //成功加入到Room时触发此事件 。
 
  {
 
  Debug.Log(“Room ” + room.GetName() + “ joined successfully”);
 
  lock (messagesLocker) {
 
  messages.Clear();
 
  }
 
  }
 
  void OnPublicMessage(string message, User sender, int roomId)         //当收到一条公有信息时触发此事件 。
 
  {
 
  // We use lock here to ensure cross-thread safety on the messages collection
 
  lock (messagesLocker) {
 
  //~ gSkin.color=Color.red;
 
  messages.Add(sender.GetName() + “ said ” + message);         //
 
  //~ gSkin.color=Color.white;
 
  }
 
  chatScrollPosition.y = Mathf.Infinity;
 
  Debug.Log(“User ” + sender.GetName() + “ said: ” + message);
 
  }
 
  void OnPrivateMessage(string message1, User sender, int roomId1,int userId)        //当收到一条私有信息时触发的事件
 
  {
 
  lock (messagesLocker) {
 
  Room currRoom = smartFox.GetRoom(roomId1);
 
  User u=currRoom.GetUser(userId);
 
  // if(u.GetName()!=smartFox.myUserName)
 
  // {
 
  messages.Add(u.GetName() + “ said ” + message1);         //
 
  // }
 
  }
 
  chatScrollPosition.y = Mathf.Infinity;
 
  Debug.Log(“User ” + sender.GetName() + “ said: ” + message1);
 
  }
 
  public Rect windowRect =new Rect (20, 20, 300, 200);
 
  void DoMyWindow ( int windowID ) {
 
  QJ_userPrivatemsg=GUI.TextArea(new Rect(10,25,280,140),QJ_userPrivatemsg);
 
  if(GUI.Button(new Rect(235,170,60,25),“发送”))
 
  {
 
  if(QJ_userPrivatemsg.Length>0)
 
  {
 
  smartFox.SendPrivateMessage(QJ_userPrivatemsg,QJ_userid,QJ_userroomid);
 
  // messages.Add(smartFox.myUserName+ “ said ” +QJ_userPrivatemsg);
 
  }
 
  QJ_userPrivatemsg=“”;
 
  windowsKG=false;
 
  }
 
  GUI.DragWindow (new Rect (0,0, 10000, 30));
 
  }
 
  // Finally draw all the lobby GUI
 
  void OnGUI()                          //
 
  {
 
  GUI.skin = gSkin;
 
  if(windowsKG)
 
  {
 
  windowRect = GUI.Window (0, windowRect, DoMyWindow, “发私聊信息给”+QJ_username);
 
  }
 
  // Login
 
  if (!isLoggedIn)                        //如果没有连接,出现输入房间名,用户名,密码
 
  {
 
  GUI.Label(new Rect(10, 90, 100, 100), “Zone: ”);
 
  zone = GUI.TextField(new Rect(100, 90, 200, 30), zone, 25);
 
  GUI.Label(new Rect(10, 126, 100, 100), “用户名: ”);
 
  username = GUI.TextField(new Rect(100, 126, 200, 30), username, 25);
 
  GUI.Label(new Rect(10, 162, 100, 100), “密码: ”);
 
  password = GUI.TextField(new Rect(100, 162, 200, 30), password, 4);
 
  //GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
 
  if (GUI.Button(new Rect(100, 200, 100, 24), “进入”) || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
 
  {
 
  smartFox.Login(zone, username, password);
 
  }
 
  }
 
  else
 
  {
 
  if(ESC)
 
  {
 
  //~ // Standard view
 
  F=false;
 
  if (GUI.Button(new Rect(200, 150, 200, 100), “退出”))                    //离开按钮
 
  {
 
  smartFox.Logout();
 
  }
 
  }
 
  // Basic info
 
  //GUI.Label(new Rect(10, 40, 200, 100), “Logged in as ” + smartFox.myUserName);
 
  Room currentActiveRoom = smartFox.GetActiveRoom();
 
  if (currentActiveRoom != null)
 
  {
 
  GUI.Label(new Rect(10, 10, 300, 60), “当前所在房间: ” + currentActiveRoom.GetName());      //当前房间
 
  }
 
  if(F)
 
  {
 
  // Room list
 
  if (roomListReceived)                               //true
 
  {
 
  GUI.Box(new Rect(100, 150, 500, 300), “房间列表”);                    //
 
  GUILayout.BeginArea (new Rect(120, 200, 460, 200));                   //
 
  roomSelection = GUILayout.SelectionGrid (roomSelection, roomStrings, 1);     //
 
  //~ roomSelection = GUILayout.SelectionGrid (roomSelection, roomStrings, 1, “RoomListButton”);
 
  if (roomStrings[roomSelection] != currentActiveRoom.GetName())
 
  {
 
  smartFox.JoinRoom(roomStrings[roomSelection]);
 
  if(roomStrings[roomSelection]==“The Hall”)
 
  {
 
  Application.LoadLevel(“room2”);
 
  }
 
  else if (roomStrings[roomSelection]==“The Kitchen”)
 
  {
 
  Application.LoadLevel(“room2”);
 
  }
 
  else if(roomStrings[roomSelection]==“The Garden”)
 
  {
 
  Application.LoadLevel(“room3”);
 
  }
 
  else
 
  {
 
  Application.LoadLevel(“room4”);
 
  }
 
  F=false;
 
  }
 
  GUILayout.EndArea();
 
  }
 
  }
 
  // Room chat window0.
 
  if(!F)
 
  {
 
  if (currentActiveRoom != null)
 
  {
 
  // User list
 
  GUI.BeginGroup(new Rect(500, 50, 180, 400), “”);
 
  if (GUI.Button(new Rect(0,0,180,30),“当前房间用户列表”))
 
  {
 
  if(HY==true)
 
  HY=false;
 
  else
 
  HY=true;
 
  }
 
  if(HY){
 
  GUI.Box(new Rect(0, 0, 180, 400),“”);
 
  GUILayout.BeginArea (new Rect(10, 20, 150, 160));
 
  userScrollPosition = GUILayout.BeginScrollView (userScrollPosition, GUILayout.Width (150), GUILayout.Height (160));
 
  GUILayout.BeginVertical();
 
  //~ userSelection = GUILayout.SelectionGrid (userSelection, userStrings, 1);
 
  foreach (User user in currentActiveRoom.GetUserList().Values)
 
  {
 
  if(GUILayout.Button(user.GetName()))
 
  {
 
  QJ_userid=user.GetId();
 
  QJ_username=user.GetName();
 
  QJ_userroomid=currentActiveRoom.GetId();
 
  if(QJ_username==smartFox.myUserName)
 
  {
 
  windowsKG=false;
 
  }
 
  else
 
  {
 
  windowsKG=true;
 
  }
 
  }
 
  }
 
  //~ GUILayout.EndArea();
 
  GUILayout.EndVertical();
 
  GUILayout.EndScrollView ();
 
  GUILayout.EndArea();
 
  }
 
  GUI.EndGroup();
 
  // Chat history
 
  GUI.Box(new Rect(10, 300, 470, 170), “聊天面板”);
 
  GUILayout.BeginArea (new Rect(20, 320, 450, 130));
 
  chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (450), GUILayout.Height (130));
 
  GUILayout.BeginVertical();
 
  // We use lock here to ensure cross-thread safety on the messages collection
 
  lock (messagesLocker)
 
  {
 
  foreach (string message in messages)
 
  {
 
  //f(smartFox.myUserName!=user.GetName()){
 
  GUILayout.Label(message);
 
  //}
 
  }
 
  }
 
  GUILayout.EndVertical();
 
  GUILayout.EndScrollView ();
 
  GUILayout.EndArea();
 
  // Send message
 
  newMessage = GUI.TextField(new Rect(10, 480, 370, 30), newMessage, 50);
 
  if ((GUI.Button(new Rect(390, 478, 90, 30), “发送”) || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))&&newMessage.Length>0)
 
  {
 
  smartFox.SendPublicMessage(newMessage, currentActiveRoom.GetId());
 
  newMessage = “”;
 
  }
 
  if (GUI.Button(new Rect(500, 478, 180, 30), “显示房间列表”))                    //离开按钮
 
  {
 
  F=true;
 
  }
 
  }
 
  }
 
  }
 
  }
 
  }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小姑娘很大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值