This is quite complex, but I'll try.
I have a static class called "Networking", inside I have various classes for all requests.
When opening the app I do the version check as #Guest_N to # Extension_1, and after the response from the server I disconnect it.
The user finds himself in a screen with:
- Registration
- Login
- Forgot password
For each request that the user makes:
1) First I do another version check like #Guest_N all #Extension_1, after the response from the server I disconnect it.
2) I make the request of #Guest_N to # Extension_1 and then I disconnect it.
At login the user from #Guest_N becomes #User in the # Extension_2.
For the Logout instead I have various points in the code where a user can logout:
- OnApplicationQuit()
- User inactivity
- Logout in the settings panel
- Loss of connection
I each of these requests when I unlink #Guest_N I call this method:
Code: Select all
public static void Disconnect()
{
if (sfsConnection != null && sfsConnection.IsConnected)
{
sfsConnection.RemoveAllEventListeners();
sfsConnection.Disconnect();
sfsConnection = null;
}
}
While when the #Guest_N make the requests I log in like this:
Code: Select all
public class Control_Version
{
public static void controlVersion() //It starts from here
{
Debug.Log("Controllo versione " + Application.version);
sfsConnection = new SmartFox();
sfsConnection.ThreadSafeMode = true;
sfsConnection.AddEventListener(SFSEvent.CONNECTION, OnVersionConnection);
sfsConnection.AddEventListener(SFSEvent.CRYPTO_INIT, OnCryptoInit);
sfsConnection.Connect(ServerIP, ServerPort);
}
public static void OnCryptoInit(BaseEvent evt)
{
if ((bool)evt.Params["success"])
{
Debug.Log("Control_Version OnCryptoInit TRUE");
// Send a login request
DoLogin();
}
else Debug.Log("Control_Version OnCryptoInit FALSE");
}
public static void DoLogin()
{
Debug.Log("Control_Version DoLogin");
sfsConnection.AddEventListener(SFSEvent.LOGIN, OnLoginVersion);
sfsConnection.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginVersionError);
sfsConnection.Send(new LoginRequest("", null, "Extension_1"));
}
private static void OnVersionConnection(BaseEvent evt)
{
if ((bool)evt.Params["success"])
{
Debug.Log("OnVersionConnection");
if (useEncryption) sfsConnection.InitCrypto();
else DoLogin();
}
else
{
ResetListeners();
//Show panel Warning Error Connection
//........
//........
}
}
private static void OnLoginVersion(BaseEvent evt)
{
Debug.Log("OnLoginVersion");
sfsConnection.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
ISFSObject objOut = new SFSObject();
objOut.PutUtfString("version", Application.version);
sfsConnection.Send(new ExtensionRequest("version", objOut));
}
private static void OnLoginVersionError(BaseEvent evt)
{
ResetListeners();
//Show panel Warning Error Connection
//........
//........
}
public static void OnExtensionResponse(BaseEvent evt)
{
ResetListeners();
if ((string)evt.Params["cmd"] == "version")
{
ISFSObject myData = (SFSObject)evt.Params["params"];
Debug.Log(myData.GetDump());
switch (myData.GetByte("type"))
{
case 1: //Maintenance in progress
//My code
break;
case 2: //Control Version
//My code
break;
}
}
}
private static void ResetListeners()
{
Debug.Log("ResetListeners");
sfsConnection.RemoveEventListener(SFSEvent.CONNECTION, OnVersionConnection);
sfsConnection.RemoveEventListener(SFSEvent.CRYPTO_INIT, OnCryptoInit);
sfsConnection.RemoveEventListener(SFSEvent.LOGIN, OnLoginVersion);
sfsConnection.RemoveEventListener(SFSEvent.LOGIN_ERROR, OnLoginVersionError);
sfsConnection.RemoveEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
Disconnect();
}
}