• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Easelm · Dec 12, 2012 at 10:40 PM · c#smartfox

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!

Comment

People who like this

0 Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Easelm · Dec 13, 2012 at 07:05 AM 0
Share

Anyone awake? D:

avatar image AlucardJay Easelm · Dec 13, 2012 at 07:05 AM 0
Share

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

avatar image Easelm Easelm · Dec 13, 2012 at 07:21 AM 0
Share

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. ;)

avatar image AlucardJay Easelm · Dec 13, 2012 at 07:28 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by DavidStudio · Sep 14, 2015 at 10:34 AM

@Easelm:

Before Line 55, try add below code line:

 smartFox.ThreadSafeMode = true;

Everything should be fine now. More detailed explanation can be found here.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Chat window not displayed 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

does not implement right interface 1 Answer

Renderer on object disabled after level reload 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges