Hey Guys,
I’ve written my own Script to connect with my SmartfoxX2 Server…but it always says: “Can’t connect”.
here is the script:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Requests;
using Sfs2X.Logging;
public class Login : MonoBehaviour {
public string ip = "127.0.0.1";
public int port = 9933;
private string status = "";
private SmartFox smartFox;
public Rect loginWindowRect;
private string username = "";
private string zone = "BasicExamples";
private bool loggedIn = false;
private string password = "";
private string [] rooms;
private bool isJoining = false;
// Use this for initialization
void Start () {
smartFox = new SmartFox(true);
smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
smartFox.AddEventListener(SFSEvent.LOGOUT, OnLogout);
smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
smartFox.Connect(ip, port);
}
void FixedUpdate () {
smartFox.ProcessEvents();
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
loginWindowRect = GUI.Window(0, loginWindowRect, loginWindowFunc, "Login");
}
void loginWindowFunc (int windowID) {
GUI.Label(new Rect(10, 20, 70, 20), "Username: ");
username = GUI.TextField (new Rect(80, 20, 80, 20), username, 20);
if (GUI.Button(new Rect(60, 270, 80, 20), "Login")) {
if (username != "") {
smartFox.Send(new LoginRequest(username, password, zone));
Debug.Log("Sending Login Request...");
}
else{
Debug.Log("Don't let Username empty!!!");
}
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
public void OnConnection (BaseEvent e) {
bool success = (bool)e.Params["success"];
if (success) {
Debug.Log("Succesful connected");
}
else {
Debug.Log("Can't connect");
}
}
public void OnConnectionLost (BaseEvent e) {
Debug.Log("Connection Lost");
}
public void OnApplicationQuit () {
smartFox.Disconnect();
}
public void OnLogin (BaseEvent e) {
loggedIn = true;
if (smartFox.RoomManager.ContainsRoom("Game Room")) {
smartFox.Send(new JoinRoomRequest("Game Room"));
}
else {
RoomSettings settings = new RoomSettings("Game Room");
settings.MaxUsers = 20;
smartFox.Send(new CreateRoomRequest(settings, true));
}
}
public void OnLoginError (BaseEvent e) {
}
public void OnLogout (BaseEvent e) {
Debug.Log("Sucessfully logged out...");
loggedIn = false;
}
public void OnJoinRoom (BaseEvent e) {
isJoining = false;
smartFox.RemoveAllEventListeners();
Application.LoadLevel("Game");
}
}
Thanks
BusterBlader