using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Text; using Sfs2X; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Requests; using Sfs2X.Logging; public class LobbyGUI2 : MonoBehaviour { public string serverName = "87.98.158.40"; public int serverPort = 9933; private SmartFox smartFox; private string zone = "GameLobby"; private string username = ""; private string password = ""; private string loginErrorMessage = ""; private bool isLoggedIn; private bool isJoining = 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; public GUISkin gSkin; private Room currentActiveRoom; void Start() { Security.PrefetchSocketPolicy(serverName, serverPort); bool debug = true; if (SmartFoxConnection.IsInitialized) { smartFox = SmartFoxConnection.Connection; } else { smartFox = new SmartFox(debug); } // Register callback delegate smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection); smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost); smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin); smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError); smartFox.AddEventListener(SFSEvent.LOGOUT, OnLogout); smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom); smartFox.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage); smartFox.AddLogListener(LogLevel.DEBUG, OnDebugMessage); smartFox.Connect(serverName, serverPort); Debug.Log(Application.platform.ToString()); } void FixedUpdate() { smartFox.ProcessEvents(); } private void UnregisterSFSSceneCallbacks() { // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene smartFox.RemoveAllEventListeners(); } public void OnConnection(BaseEvent evt) { bool success = (bool)evt.Params["success"]; // string error = (string)evt.Params["errorMessage"]; // Debug.Log("On Connection callback got: " + success + " (error : <" + error + ">)"); if (success) { SmartFoxConnection.Connection = smartFox; } } public void OnConnectionLost(BaseEvent evt) { Debug.Log("OnConnectionLost"); isLoggedIn = false; isJoining = false; currentActiveRoom = null; UnregisterSFSSceneCallbacks(); } // Various SFS callbacks public void OnLogin(BaseEvent evt) { try { bool success = true; if (evt.Params.ContainsKey("success") && !(bool)evt.Params["success"]) { loginErrorMessage = (string)evt.Params["errorMessage"]; Debug.Log("Login error: "+loginErrorMessage); } else { isLoggedIn = true; Debug.Log("Logged in successfully"); ReadRoomListAndJoin(); } } catch (Exception ex) { Debug.Log("Exception handling login request: "+ex.Message+" "+ex.StackTrace); } } public void OnLoginError(BaseEvent evt) { Debug.Log("Login error: "+(string)evt.Params["errorMessage"]); } void OnLogout(BaseEvent evt) { Debug.Log("OnLogout"); isLoggedIn = false; isJoining = false; currentActiveRoom = null; } public void OnDebugMessage(BaseEvent evt) { string message = (string)evt.Params["message"]; Debug.Log("[SFS DEBUG] " + message); } private void ReadRoomListAndJoin() { Debug.Log("Room list: "); List roomList = smartFox.RoomManager.GetRoomList(); List roomNames = new List(); foreach (Room room in roomList) { if (room.IsHidden || room.IsPasswordProtected) { continue; } roomNames.Add(room.Name); Debug.Log("Room id: " + room.Id + " has name: " + room.Name); } // roomStrings = roomNames.ToArray(); if (smartFox.LastJoinedRoom==null) { JoinRoom("The Lobby"); } } void JoinRoom(string roomName) { if (isJoining) return; isJoining = true; currentActiveRoom = null; Debug.Log("Joining room: "+roomName); // Need to leave current room, if we are joined one if (smartFox.LastJoinedRoom==null) smartFox.Send(new JoinRoomRequest(roomName)); else smartFox.Send(new JoinRoomRequest(roomName, "", smartFox.LastJoinedRoom.Id)); } void OnJoinRoom(BaseEvent evt) { Room room = (Room)evt.Params["room"]; Debug.Log("Room " + room.Name + " joined successfully"); lock (messagesLocker) { messages.Clear(); } currentActiveRoom = room; isJoining = false; } void OnPublicMessage(BaseEvent evt) { try { string message = (string)evt.Params["message"]; User sender = (User)evt.Params["sender"]; // We use lock here to ensure cross-thread safety on the messages collection lock (messagesLocker) { messages.Add(sender.Name + " :_ " + message); } chatScrollPosition.y = Mathf.Infinity; Debug.Log("User " + sender.Name + " :_ " + message); } catch (Exception ex) { Debug.Log("Exception handling public message: "+ex.Message+ex.StackTrace); } } // Finally draw all the lobby GUI void OnGUI() { if (smartFox == null) return; GUI.skin = gSkin; GUI.Label(new Rect(2, -2, 680, 70), "", "SFSLogo"); if (!smartFox.IsConnected) { GUI.Label(new Rect(10, 90, 100, 100), "Connecting..."); } // Login else if (!isLoggedIn) { // GUI.Label(new Rect(10, 90, 100, 100), "Zone: "); // zone = GUI.TextField(new Rect(100, 90, 200, 20), zone, 25); GUI.Label(new Rect(10, 116, 100, 100), "Username: "); username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25); // GUI.Label(new Rect(10, 142, 100, 100), "Password: "); // password = GUI.TextField(new Rect(100, 142, 200, 20), password, 4); GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage); if (GUI.Button(new Rect(100, 146, 100, 24), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) { Debug.Log("Sending login request"); smartFox.Send(new LoginRequest(username, password, zone)); } } else if (isJoining) { // Standard view if (GUI.Button(new Rect(580, 700, 90, 24), "Logout")) { smartFox.Send( new LogoutRequest() ); } GUI.Label(new Rect(498, 248, 180, 40), "Joining room"); } else if (currentActiveRoom!=null) { // Standard view if (GUI.Button(new Rect(580, 700, 90, 24), "Logout")) { smartFox.Send( new LogoutRequest() ); } //GUI.Label(new Rect(498, 248, 180, 40), "Current room: " + currentActiveRoom.Name); // User list GUI.Box(new Rect(490, 80, 180, 170), "Users"); GUILayout.BeginArea (new Rect(500, 100, 150, 160)); userScrollPosition = GUILayout.BeginScrollView (userScrollPosition, GUILayout.Width (150), GUILayout.Height (160)); GUILayout.BeginVertical(); foreach (User user in currentActiveRoom.UserList) { GUILayout.Label(user.Name); } GUILayout.EndVertical(); GUILayout.EndScrollView (); GUILayout.EndArea(); // Chat history //GUI.Box(new Rect(10, 80, 470, 390), "Chat"); GUILayout.BeginArea (new Rect(20, 110, 450, 350)); chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (450), GUILayout.Height (350)); GUILayout.BeginVertical(); // We use lock here to ensure cross-thread safety on the messages collection lock (messagesLocker) { foreach (string message in messages) { GUILayout.Label(message); } } GUILayout.EndVertical(); GUILayout.EndScrollView (); GUILayout.EndArea(); // Send message newMessage = GUI.TextField(new Rect(10, 700, 370, 20), newMessage, 50); if (GUI.Button(new Rect(390, 700, 90, 24), "Send") || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) { smartFox.Send( new PublicMessageRequest(newMessage) ); newMessage = ""; } } } }