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