Login into a zone

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Maks
Posts: 2
Joined: 29 Jan 2020, 04:56

Login into a zone

Postby Maks » 29 Jan 2020, 05:15

Hello Can someone Help me,please? I have the smartfoxserver version: 2.14.0 and Unity version 2019.2.19f1 Personal. I just imported dll file(C# API) v 1.7.12 in plugins folder. Created the zone with the name of "game" in smartfox. After that i wrote the code on client side for Connection to smartfox and Login into a zone "game" using smartfox and unity video lessons.Unity client was succesfully connected to the smartfox but Login Logs didn't appear on console. What mistakes i have made?The same problems is for SignUp script,i wrote exactly the same code as was mentioned in video by GenesisRage. Here is the following 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.Requests;
public class Login : MonoBehaviour
{
 
    public string Host = "127.0.0.1";
    public int TcpPort = 9933;

    public string Zone = "game";
    public string UserName = "User_";
    SmartFox sfs;
    // Start is called before the first frame update
    public void Start()
    {

        ConfigData cfg = new ConfigData();
        cfg.Host = Host;
        cfg.Port = TcpPort;
        cfg.Zone = Zone;
        sfs = new SmartFox();
        Debug.Log("SFS2X C# API v" + sfs.Version);
        //Event Listeners
        sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
        sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
        sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);

        //Connection to the smartfox
        sfs.Connect(cfg);
        sfs.Send(new LoginRequest(UserName, "", Zone));
        //login in to Zone

    }

    public void OnLogin(BaseEvent evt)
    {
        User user = (User)evt.Params["user"];
        Debug.Log("User: " + user.Name);


    }
    void OnLoginError(BaseEvent evt)
    {
        Debug.Log("Login error (" + evt.Params["errorCode"] + ") " + evt.Params["errorMessage"]);
    }
    void OnConnection(BaseEvent evt)
    {
        if ((bool)evt.Params["success"])
        {
            Debug.Log("Succesfully Connected");
        }
        else
        {
            Debug.Log("Connection failed");
        }
    }

    // Update is called once per frame
    public void Update()
    {
        sfs.ProcessEvents();
    }
    public void OnApplicationQuit()
    {
        if (sfs.IsConnected)
            sfs.Disconnect();
    }
}
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Login into a zone

Postby Lapo » 29 Jan 2020, 08:17

Hi,
the problem is that the login request must be sent inside the OnConnection handler method.

In other words you need to wait for the client to successfully connect before sending the Login req. What is happening now is that you're connecting and immediately trying to login without waiting for the connection to be ready.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Maks
Posts: 2
Joined: 29 Jan 2020, 04:56

Re: Login into a zone

Postby Maks » 29 Jan 2020, 09:09

Oh thank you very much,it works

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 32 guests