Smartfox freezing Unity editor and...

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

Smartfox freezing Unity editor and...

Postby m0rr0ws0n » 22 Sep 2017, 12:30

and causing scripts not to update?

This seems to be what is happening for me. I go back to old code and scripts update and there are no freezes. When I go to smartfox code, it freezes and the scripts don't update. I simply followed the guides on how to get it working and wrote an extension handler for the client and server. Here is my code:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Sfs2X;
using Sfs2X.Logging;
using Sfs2X.Util;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Entities.Data;
using Sfs2X.Requests;
using Sfs2X.Requests.MMO;
using UI.Dialogs;
using UnityEngine.SceneManagement;

public class EstablishSFS : MonoBehaviour
{

    public static string[] charList;
    public static string sessionUserName;
    public Button LoginButton;
    public Button RegisterButton;
    public Button ExitButton;
    public Text loginText;
    public InputField usernameField;
    public InputField passwordField;
    public string Host = "127.0.0.1";
    public int TcpPort = 9933;
    public string Zone = "WarCatalyst";
    public int test = 446;
    public int test2 = 8787;
    public UI.Dialogs.uDialog myDialog;

    private SmartFox sfs;

    // Use this for initialization
    void Start()
    {

        Button Login = LoginButton.GetComponent<Button>();
        Button Register = RegisterButton.GetComponent<Button>();
        Button Exit = ExitButton.GetComponent<Button>();

        Login.onClick.AddListener(OnLoginButtonPress);
        Register.onClick.AddListener(OnRegisterButtonPress);
        Exit.onClick.AddListener(OnExitButtonPress);

        myDialog.AddOnCloseEvent(onDialogClose);
    }

    private void onDialogClose(uDialog arg0)
    {

    }

    void OnLoginButtonPress()
    {
        ConfigData cfg = new ConfigData();
        cfg.Host = Host;
        cfg.Port = TcpPort;
        cfg.Zone = Zone;
        cfg.UseBlueBox = false;

        sfs = new SmartFox();

        sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
        sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
        sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
        sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
        sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnRoomJoin);
        sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnRoomJoinError);
        sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);

        sfs.Connect(cfg);
    }

    void OnRegisterButtonPress()
    {
        Application.OpenURL("http://localhost/register.php");
    }

    void OnExitButtonPress()
    {
        Application.Quit();
    }

    // Update is called once per frame
    void Update()
    {
        if (sfs != null)
        {
            sfs.ProcessEvents();
        }

    }

    private void reset()
    {
        // Remove SFS2X listeners
        sfs.RemoveAllEventListeners();
    }

    //----------------------------------------------------------
    // SmartFoxServer event listeners
    //----------------------------------------------------------

    private void OnConnection(BaseEvent evt)
    {
        if ((bool)evt.Params["success"])
        {
            // Save reference to the SmartFox instance in a static field, to share it among different scenes
            SFSInstance.Connection = sfs;
            Debug.Log("Connected Successfully to SmartFox");

            // Login
            sfs.Send(new LoginRequest(usernameField.text, passwordField.text, Zone));
        }
        else
        {
            // Remove SFS2X listeners and re-enable interface
            reset();

            // Show error message
            Debug.Log("Connection failed; is the server running at all?");
           myDialog.SetContentText("Connection to game server failed.");
           myDialog.Show();
        }
    }

    private void OnConnectionLost(BaseEvent evt)
    {
        // Remove SFS2X listeners and re-enable interface
        reset();

        string reason = (string)evt.Params["reason"];

        if (reason != ClientDisconnectionReason.MANUAL)
        {
            // Show error message
            Debug.Log("Connection was lost; reason is: " + reason);

            myDialog.SetContentText("Connection was lost; reason is: " + reason);
            myDialog.Show();

        }
    }

    private void OnLogin(BaseEvent evt)
    {
        string roomName = "World";
        Debug.Log("Logged In.");

        // join room
        if (sfs.RoomManager.ContainsRoom(roomName))
        {
            sfs.Send(new JoinRoomRequest(roomName));
        }
    }

    private void OnLoginError(BaseEvent evt)
    {
        // Disconnect
        sfs.Disconnect();

        // Remove SFS2X listeners and re-enable interface
        reset();

        // Show error message
        Debug.Log("Login failed: " + (string)evt.Params["errorMessage"]);

        myDialog.SetContentText("Login failed: " + (string)evt.Params["errorMessage"]);
        myDialog.Show();
    }

    private void OnRoomJoin(BaseEvent evt)
    {
        // Remove SFS2X listeners and re-enable interface before moving to the main game scene
        //reset();

        Debug.Log("Successfully joined World Room.");

        ISFSObject objOut = new SFSObject();
        objOut.PutText("username", usernameField.text);

        sfs.Send(new ExtensionRequest("GetCharList", objOut));

        SceneManager.LoadScene("CharSelection");
    }

    void OnExtensionResponse(BaseEvent e)
    {
        Debug.Log("in extension request handler");

        string cmd = (string)e.Params["cmd"];
        ISFSObject objIn = (SFSObject)e.Params["params"];

        string charListString = "";

        if (cmd == "GetCharList")
        {
            ISFSArray results = objIn.GetSFSArray("charList");

            for (int i = 0; i < results.Size(); i++)
            {
                ISFSObject item = results.GetSFSObject(i);
                charListString = item.GetUtfString("char1") + "," + item.GetUtfString("char2") + "," + item.GetUtfString("char3") + "," + item.GetUtfString("char4") + "," +
                    item.GetUtfString("char5");
            }

            charList = charListString.Split(',');

            Debug.Log(charList[3]);

           
        }
    }

    private void OnRoomJoinError(BaseEvent evt)
    {
        // Show error message
        Debug.Log("Room join failed: " + (string)evt.Params["errorMessage"]);

        myDialog.SetContentText("Room join failed: " + (string)evt.Params["errorMessage"]);
        myDialog.Show();
    }
}



Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;

/**
 * Singleton class with static fields to hold a reference to SmartFoxServer connection.
 * It is useful to access the SmartFox class from anywhere in the game.
 */
public class SFSInstance : MonoBehaviour
{
    private static SFSInstance mInstance;
    private static SmartFox sfs;

    public static SmartFox Connection
    {
        get
        {
            if (mInstance == null)
            {
                mInstance = new GameObject("SFSInstance").AddComponent(typeof(SFSInstance)) as SFSInstance;
            }
            return sfs;
        }
        set
        {
            if (mInstance == null)
            {
                mInstance = new GameObject("SFSInstance").AddComponent(typeof(SFSInstance)) as SFSInstance;
            }
            sfs = value;
        }
    }

    public static bool IsInitialized
    {
        get
        {
            return (sfs != null);
        }
    }

    // Handle disconnection automagically
    // ** Important for Windows users - can cause crashes otherwise
    void OnApplicationQuit()
    {
        if (sfs.IsConnected)
        {
            sfs.Disconnect();
        }
    }
}


Let me explain what happens when it freezes. I click the login button. It properly logs in, joins the zone and room, sends the extension request and processes the response and gives me my charList that I need. Then I go load the character selection screen. I see that it works and I stop the game. Now the next time I try to run the game Unity goes unresponsive and I have to end it in the task manager. It doesn't do this with my code I did in php. What could be causing this freezing?
m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

Re: Smartfox freezing Unity editor and...

Postby m0rr0ws0n » 22 Sep 2017, 13:49

ALso I forgot to mention I changed this code

SceneManager.LoadScene("CharSelection");

to OnRoomjoin for testing purposes. It is usually at the very end of OnExtensionResponse().
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Smartfox freezing Unity editor and...

Postby Lapo » 22 Sep 2017, 13:53

Hi,
we've haven't heard of a similar issues in a long time. (It used to happen using the mono debugger many years ago)

What version of Unity are you using and what is the version of the SFS2X API?
(You can print out the SmartFox.Version property if you don't remember)

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

Re: Smartfox freezing Unity editor and...

Postby m0rr0ws0n » 22 Sep 2017, 15:23

I did SmartFox.Version and it says 1.7.2. I'm using Unity 2017.1.0f3. :o
m0rr0ws0n
Posts: 11
Joined: 22 Sep 2017, 12:11

Re: Smartfox freezing Unity editor and...

Postby m0rr0ws0n » 22 Sep 2017, 15:31

OK I just let it run to see what would happen and eventually it goes into play mode and becomes responsive. It takes like 40 seconds to become responsive though. Any idea now what is causing it?

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 72 guests