- Home /
Smart Fox Server Lobby
This doesn't seem to have been asked, but I'm not 100% sure. I'm trying to make a lobby where someone can log in, then chat and choose a room to play in, which they can then go into via a "play now" button. But there are several problems: 1. Custom login I can't figure out how to make a custom login. I looked at examples but they were in css. Do I need to know that, because I'm good at Javascript? 2. Room select I made a list using GUI, but I don't know how to make the list clickable and move people into those rooms. Again, this is a beginner's kind of problem, but I couldn't figure this out through help 3. Play now After I added the play now button, I tried to see if it worked, and now I can't connect. At the admin tool it says the server is up,but I can't connect. I tried to edit the example's css, but it seems I messed up somewhere. can you show me where?
Thanks if you can help, I'm really new to Unity, and this is the first time I've used SFS to. I am familiar with Flash, and other scripting codes like Java, Ruby (1 and 2), HTML (duh), etc.
Heres the code I was trying to edit in the example lobby that (as you know) failed epically(lol did I spell this right?):
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 LobbyGUI : MonoBehaviour { public string serverName = "127.0.0.1"; public int serverPort = 9933;
private SmartFox smartFox; private string zone = "Unnamed MMO"; 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() { 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);
}
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["error"];
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<Room> roomList = smartFox.RoomManager.GetRoomList();
List<string> roomNames = new List<string>();
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("Main Room");
}
}
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 + " said " + message);
}
chatScrollPosition.y = Mathf.Infinity;
Debug.Log("User " + sender.Name + " said: " + 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;
if (!smartFox.IsConnected) {
GUI.Label(new Rect(10, 90, 100, 100), "Connecting...");
}
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, 166, 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, 478, 90, 24), "Logout"))
{
smartFox.Send( new LogoutRequest() );
}
GUI.Label(new Rect(498, 248, 180, 40), "Joining room");
}
else if (currentActiveRoom!=null)
{
if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
{
smartFox.Send( new LogoutRequest() );
}
GUI.Label(new Rect(498, 248, 180, 40), "Current room: " + currentActiveRoom.Name);
// Room list
GUI.Box(new Rect(490, 80, 180, 170), "Room List");
GUILayout.BeginArea (new Rect(500, 110, 150, 130));
roomSelection = GUILayout.SelectionGrid (roomSelection, roomStrings, 1, "RoomListButton");
if (GUI.Button(new Rect(170, 180, 80, 490), "Play Game"))
{
Application.LoadLevel(1); //is this valid css? I put this here myself.
}
if (roomStrings[roomSelection] != currentActiveRoom.Name)
{
JoinRoom(roomStrings[roomSelection]);
GUILayout.EndArea();
return;
}
GUILayout.EndArea();
// User list
GUI.Box(new Rect(490, 270, 180, 200), "Users");
GUILayout.BeginArea (new Rect(500, 300, 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, 480, 370, 20), newMessage, 50);
if (GUI.Button(new Rect(390, 478, 90, 24), "Send") || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
{
smartFox.Send( new PublicMessageRequest(newMessage) );
newMessage = "";
}
}
}
}
Answer by Steffen Franz · Jan 22, 2011 at 01:10 AM
the Smartfox Server documentation has all the info you need for this, SFS and Unity code wise. I would check over on their side. http://www.smartfoxserver.com/docs/ Even more so as you are familiar with Flash most of the documentation examples are in AS which you can easily translate into C#
Its still hard to translate anything into a script you don't know... Thank you for the help, I will try there, but if you do figure out how to do it directly, that would be great :)
o (sry for double posting) does this work for SFS 2X? Because that what I have currently
Answer by WhiskeyJack · May 16, 2011 at 07:32 PM
Better late than never. For a custom login you will need to code some stuff on the server side in java (not javascript). For an excellent tutorial on the basics of creating a serverside extension watch this video: http://www.youtube.com/watch?v=nKGxhwJ0Ccc
Also, like Steffen mentioned, take another close look at http://www.smartfoxserver.com/docs Chapter 8.3 shows how to create a simple database extension (not dealing with any javascript which you do not need at all by the way).
Answer by CarlLydon · Mar 02, 2014 at 04:48 AM
If you prefer javascript extension for your custom login you could use an older version of smartFox, which allows js.