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?