Issue with joining a room (Unity3D)

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Bax, Lapo

Issue with joining a room (Unity3D)

Postby Fishypants » 11 Dec 2010, 18:54

Ahhhhh! Been at this for a few days now, and cannot seem to get it to work. Very annoying because it seems like I am pretty close.

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!
Fishypants
 
Posts: 53
Joined: 26 Oct 2010, 14:03
Location: South Pasadena California

Postby appels » 11 Dec 2010, 22:24

if your using a custom login extension, you should post it also.
check if you login your user server side.

also :

// CONNECT
smartFox.Connect(ip, port);
smartFox.Login(zone, name, "");

you should move the login call to the OnConnection callback.
appels
 
Posts: 464
Joined: 28 Jul 2010, 02:12

Postby Fishypants » 12 Dec 2010, 06:41

Thanks for the reply apples. Ill move the Login call to where you suggested. And nope, no custom extensions, just standard connect.
Fishypants
 
Posts: 53
Joined: 26 Oct 2010, 14:03
Location: South Pasadena California

Postby Fishypants » 12 Dec 2010, 17:24

So I moved the login call to the OnConnection method, but it still seems like I am connected to the zone, and not the room. I can see that there is 1 user in "Login" zone, but no one in the "Main" room. :(
Fishypants
 
Posts: 53
Joined: 26 Oct 2010, 14:03
Location: South Pasadena California

Postby Fishypants » 12 Dec 2010, 17:55

ARRRGHHH!! ... Im an idiot. So for some reason I typed under the OnConnection method:

UnregisterSFSSceneCallbacks();

Which doesn't quite make sense considering after you connect, you kinda need those SceneCallBacks, so nothing was getting called.

SOOO I made another method called RegisterSFSSceneCallbacks() and actually += all the callbacks and now it works.

:lol:
Fishypants
 
Posts: 53
Joined: 26 Oct 2010, 14:03
Location: South Pasadena California

Postby ThomasLund » 12 Dec 2010, 18:24

(thumbs up)
ThomasLund
 
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden


Return to .Net / Unity3D API

Who is online

Users browsing this forum: No registered users and 1 guest