Ok so I am running though this tutorial on you tube Unity: SmartFox (006): Setup and Install Extension - YouTube and its working great but I realized something when I was fooling around with the variables for the public message. Everything is working just fine but the number of guest do not decrease when I stop the game. It just keeps adding up one at a time when i test the game out. I followed the code exactly but my Guest# keeps increasing. I think once I close the application it should minus one of the guest should it not?
is it not what this method does?
//safely disconnect so no lose connection
void OnApplicationQuit()
{
if(sfs.IsConnected)
{
sfs.Disconnect();
}
}
Well thanks for any help with this.
here is the whole code:
using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Entities;
public class SFS2X_Connect1 : MonoBehaviour {
public string ConfigFile = "Scripts/Network/sfs-config.xml";
public bool UseConfigFile = false;
public string ServerIP = "127.0.0.1"; //change this to remote ip for lan connection
public int ServerPort = 9933; //defult port smartfox listens to
//zone variable
//the string is a zone running on Smartfox server
public string ZoneName = "SmartFox Videos";
public string RoomName = "Lobby";
//login varaible
public string UserName = "";
//smartfox object
SmartFox sfs;
// Use this for initialization
void Start () {
sfs = new SmartFox();
//make sure smartfox uses unity tread modes
//tells smartfox to hold onto event until asked for them
sfs.ThreadSafeMode = true;
sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
//config file event listener
sfs.AddEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, OnConfigLoad);
sfs.AddEventListener(SFSEvent.CONFIG_LOAD_FAILURE, OnConfigFail);
//room joining events
sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
//send public meesage
sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE,OnPublicMessage);
//this loads server zone and stuff from an xml file
if(UseConfigFile)
{
sfs.LoadConfig(Application.dataPath + "/" + ConfigFile);
}else
{
//create connection
sfs.Connect(ServerIP, ServerPort);
}
}
//smartfox event
//must have a base event
void OnConnection(BaseEvent e)
{
if((bool)e.Params["success"])
{
Debug.Log("Successfully connected");
//set zone name to what ever in config file then log into zone
if(UseConfigFile)
{
ZoneName = sfs.Config.Zone;
}
//login request is Username password, zonename
sfs.Send(new LoginRequest(UserName, "", ZoneName));
}else
{
Debug.Log("Connection Failed");
}
}
//if login was successful
void OnLogin(BaseEvent e)
{
Debug.Log("Loged in: " + e.Params["user"]);
//join a room
//must be done after conneced to a zone and server
sfs.Send(new JoinRoomRequest(RoomName));
}
//if login was not successful
void OnLoginError(BaseEvent e)
{
Debug.Log("Login Error: " + e.Params["errorCode"] + " : " + e.Params["errorMessage"]);
}
void OnPublicMessage(BaseEvent e)
{
Room room = (Room)e.Params["room"];
User sender = (User)e.Params["sender"];
Debug.Log("[" + room.Name + "] " + sender.Name + " : " + e.Params["message"] );
}
void OnJoinRoom(BaseEvent e)
{
Debug.Log("Joined Room: " + e.Params["room"]);
//to send a public message you must join a room first
//then use this
sfs.Send(new PublicMessageRequest("Hello"));
}
void OnJoinRoomError(BaseEvent e)
{
Debug.Log("Join Room Error: " + e.Params["errorCode"] + " : " + e.Params["errorMessage"]);
}
//methods if using a config file
void OnConfigLoad(BaseEvent e)
{
Debug.Log("Config File loaded");
//maually connect to server
sfs.Connect(sfs.Config.Host,sfs.Config.Port);
}
void OnConfigFail(BaseEvent e)
{
Debug.Log("Failed to load config file");
}
// Update is called once per frame
void Update () {
//ask for threads
sfs.ProcessEvents();
//OnApplicationQuit();
}
//safely disconnect so no lose connection
void OnApplicationQuit()
{
if(sfs.IsConnected)
{
sfs.Disconnect();
}
}
}