SmartFoxServer2X Using JavaScript
I know many (like me) have tried to get SFS2X to work on Unity using JavaScript but failed, and that learning C# is too much when all your projects are already in JavaScript.
I have searched all over the net for some code example or anything to take as a guide into this, but with no luck.
So, instead of just surrendering I tried it for myself, with no guide or anything.
After more or less a day and a half I managed to make SFS2X work using JavaScript.
I followed this video tutorials on C# on how to make the basic connection and Zone/Room joining and adapted them to JavaScript: http://www.youtube.com/playlist?list=PLAD3A1B608ACA09C0
I also recreated the Object Movement example from SFS website:
http://docs2x.smartfoxserver.com/ExamplesUnity/object-movement
Here is a package with the project: 1077428–40242–$SFSX2 Java.unitypackage (4.04 MB)
Hope this helps.
Here is the code to make a Basic Connection and Zone/Room join:
All you have to do is add the SmartFox.API to your project.
#pragma strict
import Sfs2X;
import Sfs2X.Core;
import Sfs2X.Requests;
var sfs = new SmartFox();
var Server : String = "127.0.0.1";
var Port : int = 9933;
var Username : String= "";
var ZoneName : String = "BasicExamples";
var RoomName : String = "The Lobby";
function Start ()
{
sfs.ThreadSafeMode = true;
sfs.AddEventListener(SFSEvent.CONNECTION,OnConnection);
sfs.AddEventListener(SFSEvent.LOGIN,OnLogin);
sfs.AddEventListener(SFSEvent.LOGIN_ERROR,OnLoginError);
sfs.AddEventListener(SFSEvent.ROOM_JOIN,OnJoinRoom);
sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR,OnJoinRoomError);
}
function OnGUI()
{
Username = GUI.TextField(Rect(300,330,100,25),Username,25);
if(GUI.Button(Rect(300,300,100,25),"Connect"))
{
sfs.Connect(Server,Port);
}
}
function FixedUpdate ()
{
sfs.ProcessEvents();
}
function OnLogin(e : BaseEvent)
{
Debug.Log("Logged as: " + e.Params["user"]);
sfs.Send(new JoinRoomRequest(RoomName));
}
function OnJoinRoom(e : BaseEvent)
{
Debug.Log("Joined Room: " + e.Params["room"]);
sfs.RemoveAllEventListeners();
Application.LoadLevel(1);
}
function OnJoinRoomError(e : BaseEvent)
{
Debug.Log("Error #" + e.Params["errorCode"] + " " + e.Params["errorMessage"]);
}
function OnLoginError(e : BaseEvent)
{
Debug.Log("Error #" + e.Params["errorCode"] + ": " + e.Params["errorMessage"]);
}
function OnConnection(e : BaseEvent)
{
if(e.Params["success"])
{
Debug.Log("Succesfully Connected");
NetWorkHolder.Connection = sfs;
sfs.Send(LoginRequest(Username,"",ZoneName));
}
else
{
Debug.Log("Not Connected");
}
}
function OnApplicationQuit()
{
if(sfs.IsConnected)
sfs.Disconnect();
}