Need Help a littlebit

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

Moderators: Lapo, Bax

diohun
Posts: 6
Joined: 23 Jan 2018, 09:24

Need Help a littlebit

Postby diohun » 25 Nov 2019, 11:53

Code: Select all

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
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;

public class SFS2X_Connect : MonoBehaviour
{
    public string ServerIP = "127.0.0.1";
    public int ServerPort = 9933;
    public InputField UserName;
    public InputField Password;
    public string ZoneName = "Europe";
    public Button LoginButton;
    public Text errorText;

    SmartFox sfs;
    // Start is called before the first frame update
    void Start()
    {
        sfs = new SmartFox();
        sfs.ThreadSafeMode = true;

        sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);

        errorText.text = "";
       


       
    }
    void OnConnection(BaseEvent e)
    {
        if ((bool) e.Params["success"]){
            Debug.Log("Succesfully Connected");
           
        }else
        {
            sfs.SetReconnectionSeconds(10);
            sfs.RemoveAllEventListeners();

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


    }
    private void OnApplicationQuit()
    {
        if (sfs.IsConnected)
            sfs.Disconnect();
    }
    public void OnLoginButtonClick()
    {
        enableLoginUI(false);

        // Set connection parameters
        ConfigData cfg = new ConfigData();
        cfg.Host = ServerIP;
#if !UNITY_WEBGL
        cfg.Port = ServerPort;
#else
      cfg.Port = WSPort;
#endif
        cfg.Zone = ZoneName;

        // Initialize SFS2X client and add listeners
#if !UNITY_WEBGL
        sfs = new SmartFox();
#else
      sfs = new SmartFox(UseWebSocket.WS_BIN);
#endif

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

        // Connect to SFS2X
        sfs.Connect(cfg);
        sfs.Send(new LoginRequest(UserName.text, Password.text, ZoneName));
    }
    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
            errorText.text = "Connection was lost; reason is: " + reason;
        }
    }
    private void reset()
    {
        // Remove SFS2X listeners
        sfs.RemoveAllEventListeners();

        // Enable interface
        enableLoginUI(true);
    }
    private void OnLoginError(BaseEvent evt)
    {
        // Disconnect
        sfs.Disconnect();

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

        // Show error message
        errorText.text = "Login failed: " + (string)evt.Params["errorMessage"];
    }

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

        // Go to main game scene
        SceneManager.LoadScene("AshTown");
    }

    private void OnRoomJoinError(BaseEvent evt)
    {
        // Show error message
        errorText.text = "Room join failed: " + (string)evt.Params["errorMessage"];
    }
    private void OnLogin(BaseEvent evt)
    {
        string roomName = "Disctrict1";

        // We either create the Game Room or join it if it exists already
        if (sfs.RoomManager.ContainsRoom(roomName))
        {
            sfs.Send(new JoinRoomRequest(roomName));
        }
        else
        {
            MMORoomSettings settings = new MMORoomSettings(roomName);
            settings.DefaultAOI = new Vec3D(25f, 1f, 25f);
            settings.MapLimits = new MapLimits(new Vec3D(-100f, 1f, -100f), new Vec3D(100f, 1f, 100f));
            settings.MaxUsers = 100;
            //settings.Extension = new RoomExtension("MMORoomDemo", "sfs2x.extension.mmo.MMORoomDemoExtension");
            sfs.Send(new CreateRoomRequest(settings, true));
        }
    }
    private void enableLoginUI(bool enable)
    {
        UserName.interactable = enable;
        Password.interactable = enable;
        LoginButton.interactable = enable;
        errorText.text = "";
    }
}

if i use this connection script on unity, and i click the button, the client connected, but never join in the room, and never send the user and password strings. anyone can help?
User avatar
Lapo
Site Admin
Posts: 21836
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Need Help a littlebit

Postby Lapo » 25 Nov 2019, 14:42

Hi,
the code is incorrect in multiple places. For example you're calling the Connect() method and immediately sending the LoginRequest, which won't work because you need to wait for the SFSEvent.CONNECTION before trying to login.

I would highly recommend to download our Example Pack for Unity and check the examples there:
https://smartfoxserver.com/download#p=examples

Additionally each example has its own tutorial:
http://docs2x.smartfoxserver.com/Exampl ... troduction

The first two examples will show you exactly how to connect, login, join rooms etc. And how to handle error conditions as well.
Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
diohun
Posts: 6
Joined: 23 Jan 2018, 09:24

Re: Need Help a littlebit

Postby diohun » 25 Nov 2019, 14:52

[quote="Lapo"]Hi,
the code is incorrect in multiple places. For example you're calling the Connect() method and immediately sending the LoginRequest, which won't work because you need to wait for the SFSEvent.CONNECTION before trying to login.

ohh thanks! :D i see :D Thanks Lapo

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 39 guests