Anyways, what I am trying to do (to help me understand smartfox better) is to recreate the login example from scratch. What I would like to do is combine everything into 1 script and have it so when you launch the Unity project, it auto connects, logs into a zone and joins a room.
Now, granted this is NOT the behavior that my game will be using, BUT I figure its a good learning experience none the less. It is at least helping me understand things better.
Ok so what works (or seems to be working):
1) Unity project starts and connection to the server is made. Number of users increases in Smartfox Admin tool.
2) I am able to join a zone (in this case "Login"), and can check that there is a user logged into that zone with the admin tool.
I for the life of me cannot figure out how to join a room called "Main" in zone "Login".
Also I put a debug message in OnRoomList(), which should be fired when the client receives the room list from the server. Not sure why it is not firing, but I am assuming that is the issue I am having.
P.S - Do I NEED to get a room list? Can I just say JoinRoom("roomname")? or is that a no-no?
-----
Anyways, here is the code I have so far:
SmartFox.cs:
Code: Select all
using UnityEngine;
using SmartFoxClientAPI;
// Statics for holding the connection to the SFS server end
// Can then be queried from the entire game to get the connection
public static class SmartFox {
private static SmartFoxClient smartFox;
public static SmartFoxClient Connection {
get { return smartFox; }
set { smartFox = value; }
}
public static bool initialized {
get{ return (smartFox != null); }
}
}
ConnectionGUI.cs:
Code: Select all
using UnityEngine;
using System;
using System.Collections; // Required for using hashtables
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;
public class ConnectionGUI : MonoBehaviour {
private SmartFoxClient smartFox;
private string ip = "76.90.76.49";
private int port = 9339;
private string zone = "Login";
private string room = "Main";
private string name = "guest";
private string statusMessage = "";
private bool isLoggedIn;
private bool roomListReceived = false;
private string[] roomStrings;
private bool isJoinedRoom = false;
void OnConnection(bool success, string error){
if(success){
// Update SmartFox static class with the new client connection
SmartFox.Connection = smartFox;
UnregisterSFSSceneCallbacks();
statusMessage = "Connection Successful!";
}
else{
statusMessage = "Can't connect! Error: " + error;
}
}
void OnDisconnect(){
statusMessage = "Connection lost / no connection to server.";
isLoggedIn = false;
roomListReceived = false;
UnregisterSFSSceneCallbacks();
}
void OnLogin(bool success, string name, string error){
Debug.Log("On Login Callback Got: " + success + " : " + error + " : " + name);
if(success){
isLoggedIn = true;
smartFox.GetRoomList();
}
else{
Debug.Log("Login ERROR: " + error);
}
}
void OnLogout(){
Debug.Log("LOGGING OUT");
isLoggedIn = false;
roomListReceived = false;
}
void OnRoomList(Hashtable roomList){
Debug.Log("ON ROOM LIST CALLED!!!!!!!!!!!!!!!!!!!");
try{
List<string> rooms = new List<string>();
foreach (int roomId in roomList.Keys){
Room room = (Room)roomList[roomId];
if(room.IsPrivate()){
continue;
}
rooms.Add(room.GetName());
}
roomListReceived = true;
roomStrings = rooms.ToArray();
if(smartFox.GetActiveRoom() == null){
smartFox.JoinRoom(room);
}
}
catch (Exception e){
Debug.Log("Room list error: " + e.Message + " " + e.StackTrace);
}
}
void OnJoinRoom(Room room){
Debug.Log("Room " + room.GetName() + " joined successfully!");
}
// Method to unregister all sfs callbacks
private void UnregisterSFSSceneCallbacks(){
// This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
SFSEvent.onConnection -= OnConnection;
SFSEvent.onConnectionLost -= OnDisconnect;
SFSEvent.onLogin -= OnLogin;
SFSEvent.onLogout -= OnLogout;
SFSEvent.onRoomListUpdate -= OnRoomList;
SFSEvent.onJoinRoom -= OnJoinRoom;
SFSEvent.onDebugMessage -= OnDebugMessage;
}
public void OnDebugMessage(string message){
Debug.Log("[SFS DEBUG] " + message);
}
// Unity methods
// ----------------------------------------------------------------------------------------------------
void Start(){
bool debug = true;
// If smartfox is already initialized, set our client to the static class connection
if(SmartFox.initialized){
smartFox = SmartFox.Connection;
}
else{
try{
smartFox = new SmartFoxClient(debug);
}
catch(Exception e){
statusMessage = e.ToString();
}
}
// Register callback delegates
SFSEvent.onConnection += OnConnection;
SFSEvent.onConnectionLost += OnDisconnect;
SFSEvent.onLogin += OnLogin;
SFSEvent.onLogout += OnLogout;
SFSEvent.onRoomListUpdate += OnRoomList;
SFSEvent.onJoinRoom += OnJoinRoom;
SFSEvent.onDebugMessage += OnDebugMessage;
// CONNECT
smartFox.Connect(ip, port);
smartFox.Login(zone, name, "");
}
void Update(){
/*
if(!isLoggedIn){
Debug.Log("Logging into zone");
smartFox.Login(zone, name, "");
}
if(!joinedRoom){
if(roomListReceived){
smartFox.JoinRoom(room);
joinedRoom = true;
}
}
*/
if(!isJoinedRoom && roomListReceived){
smartFox.JoinRoom("Main");
isJoinedRoom = true;
}
}
void OnGUI(){
GUI.Label(new Rect(10, 10, 500, 100), "Status: " + statusMessage);
}
}
If anyone could point me in the right direction, I would be very greatful. Thanks!