- Home /
SmartFox SFSEvent.CONNECTION isn't calling
Hello! I downloaded the SmartFoxServer2X examples a few weeks ago and started messing with the FPS game.
Now, when I insert the FPS game package to a new project and test it with my server, it works fine; however, when I wrote something similar to the FPS game LoginGUI code on my project, the 'OnConnection' or any of the EventListeners I added will not call at all.
Here is the code:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Requests;
using Sfs2X.Logging;
public class LoginGUI : MonoBehaviour
{
#region SmartFox
private SmartFox smartFox;
#endregion
#region Variables
private bool shuttingDown = false;
public string serverName = "XXX.XXX.XXX.XXX";
private string username = "";
private string loginErrorMessage = "";
public int serverPort = 9933;
public string zone = "Nomsoft";
public bool debug = true;
#endregion
#region Skin
public GUISkin loginGUISkin;
#endregion
void OnApplicationQuit()
{
shuttingDown = true;
}
void FixedUpdate()
{
smartFox.ProcessEvents();
}
void Awake()
{
Application.runInBackground = true;
if (Application.isWebPlayer || Application.isEditor) {
if (!Security.PrefetchSocketPolicy(serverName, serverPort, 500)) {
}
}
if (SmartFoxConnection.IsInitialized)
smartFox = SmartFoxConnection.Connection;
else
smartFox = new SmartFox();
smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
smartFox.AddEventListener (SFSEvent.UDP_INIT, OnUdpInit);
smartFox.AddLogListener (LogLevel.DEBUG, OnDebugMessage);
smartFox.Connect (serverName, serverPort);
}
#region GUI
void OnGUI()
{
GUI.skin = loginGUISkin;
if (smartFox.IsConnected)
{
DrawLoginGUI();
}
else
{
string message = "Waiting for connection";
if (loginErrorMessage != "")
{
message = "Connection error : " + loginErrorMessage;
}
DrawMessagePanelGUI(message);
}
}
// Generic single message panel
void DrawMessagePanelGUI(string message) {
// Lets just quickly set up some GUI layout variables
float panelWidth = 400;
float panelHeight = 300;
float panelPosX = Screen.width/2 - panelWidth/2;
float panelPosY = Screen.height/2 - panelHeight/2;
// Draw the box
GUILayout.BeginArea(new Rect(panelPosX, panelPosY, panelWidth, panelHeight));
GUILayout.Box ("FPS Example", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
GUILayout.BeginVertical();
GUILayout.BeginArea(new Rect(20, 25, panelWidth-40, panelHeight-60), GUI.skin.customStyles[0]);
// Center label
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
GUILayout.Label(message);
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea ();
GUILayout.EndVertical();
GUILayout.EndArea ();
}
// Login GUI allowing for username, password and zone selection
private void DrawLoginGUI() {
// Lets just quickly set up some GUI layout variables
float panelWidth = 400;
float panelHeight = 300;
float panelPosX = Screen.width/2 - panelWidth/2;
float panelPosY = Screen.height/2 - panelHeight/2;
// Draw the box
GUILayout.BeginArea(new Rect(panelPosX, panelPosY, panelWidth, panelHeight));
GUILayout.Box ("Login", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
GUILayout.BeginVertical();
GUILayout.BeginArea(new Rect(20, 25, panelWidth-40, panelHeight-60), GUI.skin.customStyles[0]);
// Lets show login box!
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
GUILayout.Label("Username: ");
username = GUILayout.TextField(username, 25, GUILayout.MinWidth(200));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Server Name: ");
serverName = GUILayout.TextField(serverName, 25, GUILayout.MinWidth(200));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Server Port: ");
serverPort = int.Parse(GUILayout.TextField(serverPort.ToString(), 4, GUILayout.MinWidth(200)));
GUILayout.EndHorizontal();
/*
GUILayout.BeginHorizontal();
GUILayout.Label("Invert Mouse Y: ");
OptionsManager.InvertMouseY = GUILayout.Toggle(OptionsManager.InvertMouseY, "");
GUILayout.EndHorizontal();*/
GUILayout.Label(loginErrorMessage);
// Center login button
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) {
Debug.Log("Sending login request");
smartFox.Send(new LoginRequest(username, "", zone));
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
GUILayout.EndArea ();
GUILayout.EndVertical();
GUILayout.EndArea ();
}
#endregion
private void UnregisterSFSSCallBacks()
{
smartFox.RemoveAllEventListeners();
}
void OnConnection(BaseEvent m_event)
{
bool success = (bool)m_event.Params["success"];
string error = (string)m_event.Params["error"];
Debug.Log("Connection : " + success);
Debug.Log("Connection Error : <" + error + ">");
loginErrorMessage = "";
if (success)
SmartFoxConnection.Connection = smartFox;
else
loginErrorMessage = error;
}
void OnLogin(BaseEvent m_event)
{
bool success = true;
if (m_event.Params.ContainsKey("success") && !(bool)m_event.Params["success"])
{
loginErrorMessage = (string)m_event.Params["errorMessage"];
Debug.Log("Error Logging In : " + loginErrorMessage);
}
else
{
Debug.Log ("Logged in Successfully");
smartFox.InitUDP (serverName, serverPort);
}
}
void OnUdpInit(BaseEvent m_event)
{
if (m_event.Params.ContainsKey("success") && !(bool)m_event.Params["success"])
{
loginErrorMessage = (string)m_event.Params["errorMessage"];
Debug.Log ("UDP Error : " + loginErrorMessage);
}
else
{
Debug.Log ("UDP is okay!");
loginErrorMessage = "";
UnregisterSFSSCallBacks ();
Application.LoadLevel ("Scene");
}
}
void OnDebugMessage(BaseEvent m_event)
{
string msg = (string)m_event.Params["message"];
Debug.Log ("[DEBUG]: " + msg);
}
void OnConnectionLost(BaseEvent m_event)
{
loginErrorMessage = "Connection Lost";
UnregisterSFSSCallBacks ();
}
}
As I said, it is very similar to the FPS game example code. I will repeat the issue in a different term:
When starting my client up, the session will be created, followed by these messages (ServerSide):
12 Dec 2012 | 22:28:46,242 | INFO | SocketReader | bitswarm.core.SocketAcceptor | Session created: { Id: 23, Type: DEFAULT, Logged: No, IP: 74.222.108.108:61799 } on Server port: 9933 <---> 61799
Okay, the session is created, but nothing happens on the Client side. As you see, the Debug messages are suppose to appear once the function is called when it connects.. but it does not call.
Any information will be valuable towards my problem!
Hey there, I think you may have bamboozled a large portion of the community (well, me anyway). However if you would like to bump your question, please use the comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Under the answer where it says edit | delete | more , click on more , then convert to comment
Good Luck
Elaborate on the part where I "bamboozled" a large portion of the community? Quite a big implication there. Anyway, I think I'll just wait. The 'answer' wasn't bumping the answer, it was just a mere "Anyone here so you can answer my question?" kind of thing. ;)
Yes, but the point remains the same, Answer fields are for answers only, as this is a knowledge base.
And yes, my comment was probably over-friendly, but it was meant with tongue-in-cheek, as many of the experienced and vastly knowledged people would know me to be a over enthusiastic but far from knowledgeable. So I try to help where I can by going through the moderation que, and help people understand how this 'site works, there are occasions where answers that are supposed to be comments are downvoted.
Answer by DavidStudio · Sep 14, 2015 at 10:34 AM
Before Line 55, try add below code line:
smartFox.ThreadSafeMode = true;
Everything should be fine now. More detailed explanation can be found here.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Chat window not displayed 1 Answer
Multiple Cars not working 1 Answer
How do I load customized characters into my next scene? 2 Answers
C# how to have a part of an object rotate while animation is being played 2 Answers