No, that’s not found in my script at all, but that is definitely something I didn’t even know about… could be useful in the future. Here is my complete LobbyGUI script, modified from the one provided with SmartFox2X. There is a few lines that enable other scripts after a connection is made, but you’ll see that my password variable is private, so they aren’t affecting it at all.
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 domain = “www.mydomain.com”;
//this local address is set to my remote server in the project inspector
public string serverName = “127.0.0.1”;
public int serverPort = 9933;
public SmartFox smartFox;
private string zone = "myZone";
private string username = "geekedatbirth";
private string password = "ABC123";
string myUrl = "";
[HideInInspector] public string defaultRoom = "Lobby";
private string loginErrorMessage = "";
public bool isLoggingIn;
public bool isLoggedIn;
private bool isJoining = false;
private string newMessage = "";
public ArrayList messages = new ArrayList();
// Locker to use for messages collection to ensure its cross-thread safety
public System.Object messagesLocker = new System.Object();
private Vector2 chatScrollPosition, userScrollPosition;
private int roomSelection = 0;
public string [] roomStrings;
public GUISkin gSkin;
public Room currentActiveRoom;
public int user_id = 1;
public void sendChatMessage(string msg) {
smartFox.Send( new PublicMessageRequest(msg) );
}
void OnGUI() {
if (smartFox == null) return;
if (!smartFox.IsConnected) {
// if not connected, show connecting information...
//GUI.Label(new Rect(10, 90, 100, 100), "Connecting...");
}
// Login
else if (!isLoggedIn && !isLoggingIn) {
//here, I print the username and password so I can compare them to the username and password in the database... they both match exactly, to the point that I have copied the text directly from the database and used that... till keeps telling me my password is incorrect
print("sending login request - user = '"+username+"' & pass = '"+password+"'");
isLoggingIn = true;
smartFox.Send(new LoginRequest(username, password, zone));
}
//temporary labels to show some variables
GUI.Label(new Rect(40, 50, 200, 30), "pass = "+password);
GUI.Label(new Rect(40, 100, 400, 30), "url = "+myUrl);
GUI.Label(new Rect(40, 150, 200, 30), "id = "+user_id);
GUI.Label(new Rect(40, 200, 400, 30), "name = "+username);
}
public void joinChatRoom() {
//smartFox.Send(new JoinRoomRequest("7 Sins", "", -1, false));
}
void Start()
{
Security.PrefetchSocketPolicy(serverName, serverPort);
//the myUrl here is to create a 'fake' Application.srcValue when not running in a browser... my browser code passes a srcValue just like the one listed below when run in a browser
myUrl = ("WebPlayer.unity3d?id=2&key=ABC123");
if(Application.isWebPlayer || Application.isEditor) {
if(Application.isWebPlayer) {
myUrl = Application.srcValue;
}
}
// my code to split the srcValue string and pull out the values for id and key
string [] split = myUrl.Split(new Char [] {'='});
string [] id = split[1].Split(new Char [] {'&'});
user_id = System.Convert.ToInt32(id[0]);
// this was to set the password to the value pulled from the srcValue string, but I thought that it may not be working correctly, so I commented it out, which leaves the password to the same value as when I first created the variable
//password = split[2].ToString();
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();
}
void OnApplicationQuit() {
Debug.Log("Quitting");
if (smartFox != null && smartFox.IsConnected)
{
smartFox.RemoveAllEventListeners();
smartFox.Disconnect();
}
}
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;
GetComponent<ChatGUI>().enabled = true;
GetComponent<PmGUI>().enabled = true;
GetComponent<ListGUI>().enabled = true;
}
}
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) {
GetComponent<ChatGUI>().roomsListed = true;
}
}
public void JoinRoom(string roomName) {
if (isJoining) return;
isJoining = true;
currentActiveRoom = null;
Debug.Log("Joining room: "+roomName);
//hide the room list
GetComponent<ChatGUI>().showHideRoomList();
// 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
}
Luckily I was momentarily smart enough to backup all my source files when I had this working with the tutorials database (structure is quite a bit different than mine) but I never tested repeatedly like I did with this code… it worked once and then I moved on. I think that unless someone has an idea as to why this is happening then I will revert back to that backup, which I hate to do because it means about 6 hours of frustration last night was for nothing.
It’s annoying the script doesn’t work, but as a program it is very worrisome to see Unity continue in what seems like a never-ending loop to connect to the SmartFox Server when I have the server shut off AND I stop the editor from playing. A few times while working on this Unity has crashed and the only error I got before the crash was “Too many threads”, which leads me to believe that somehow the connection is always staying open even when I click the play button again to stop Unity. I definitely do not want that.