I really require help or alternative about connecting a server from a Unity Web App in a restricted network
My situation
I work with Unity3D & SmartFoxServer2X and i export my Unity project to Unity Web Player application.
Our client has a restricted network.
In a NOT restricted network all the following solutions mentionned below works.
Attempt #1
First in the Unity app i use the normal standard socket of SFS2X in order to reach our server
Observation : Using the Unity Web App in this restricted network, there is no answer from the server so the connection seems to be impossible
Attempt #2
So i worked on the activation of the Bluebox solution in Unity3D in order to reach our server
Observation : Using the Unity Web App in this restricted network, it seems there is no answer from the server so the connection can't be established but the connection with bluebox freezes the Unity web app for a while (some seconds)
Attempt #3
So i made in unity a button to ping the server, but this failed to ping the server in the restricted network
Attempt #4
Then i made an html5/javascript connexion with SFS2X API. The idea was to allow unity web app to communicate with the server via an html5/javascript app in order to not be filtered.
Observation : Combining the Unity Web App (for 3D and GUI) and the Html5/ javascript app (for connexion) in this restricted network, a response from the server is received but the connection always failed with the error : is the server running at all ?
Attempt #5
Last try, I installed the flash example BattleFarm in our server. This flash example uses bluebox and normal socket. The client with a restricted network can connect after seconds and play with.
Conclusion
So i can conclude the following points :
- SFS2X is not the problem
- Our server seems to be well configured
- Perharps i improperly used the SFS2X API in Unity ?
- Perharps Unity apps are not authorized to connect out from the restricted network ?
- Perharps Flash apps are not filtered because frequently used in the web ?
Questions
Is someone have the same difficulties ?
Where are the wrong parts ?
Is someone have an example of unity web app using Smartfoxserver2X in Bluebox mode ?
Programs
My Unity Code + SFS2X API
public class TestConnexion : MonoBehaviour
{
#region Variables
//Config server
private SmartFox _smartFoxServer = null;
private string _IPServer = DataCenter.IPServer;
private int _IPPort = DataCenter.IPPort;
private string _Zone = DataCenter.Zone_Initial;
private string _Room = DataCenter.Room_Initial;
private string _scene_de_connexion = DataCenter.Connection_scene;
private string _sceneName = DataCenter.Scene_Principale;
//Server config && GUI display
private bool _allowConnection = true;
private bool _forceBlueBox = false;
private Ping p = null;
private string test1, test2 = string.Empty;
private string server_message_about_connexion = string.Empty;
private string server_message_about_login = string.Empty;
#endregion
#region Unity Calls
// Update is called once per frame
void FixedUpdate() { if (_smartFoxServer != null) { if (_smartFoxServer.IsConnected) { _smartFoxServer.ProcessEvents(); } } }
#region GUI
// Update is called once per frame
void OnGUI()
{
//window use
GUI.Window(0,
new Rect(
win_left, //Left
win_top, //Top
win_width, //Width
win_height), //Height
WindowConnection, //Function to call for GUI Display
"<size=25>" + "Connexion" + "</size>"); //Window's Title
}
void WindowConnection(int windowID)
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false);
{
GUILayout.Space(window_padding * 2);//Space
#region Set of tests about connectivity
if (GUILayout.Button("Test 1", GUILayout.Width(100))) { test1 = "Test 1 sock" + Security.PrefetchSocketPolicy(_IPServer, _IPPort); }
GUILayout.Label(test1);
GUILayout.Space(window_padding);//Space
if (GUILayout.Button("Test 2", GUILayout.Width(100))) { test2 = "Test 2 http" + Security.PrefetchSocketPolicy(_IPServer, DataCenter.HttpIPPort); }
GUILayout.Label(test2);
GUILayout.Space(window_padding);//Space
if (GUILayout.Button("Test 3", GUILayout.Width(100))) { StartCoroutine("Ping", _IPServer); }
if (p != null) GUILayout.Label("Test 3 ping" + p.ip + " " + p.isDone + " " + p.time);
GUILayout.Space(window_padding);//Space
GUILayout.Space(window_padding);//Space
_forceBlueBox = GUILayout.Toggle(_forceBlueBox, "Forcer la connection ?");
GUILayout.Space(window_padding);//Space
#endregion
#region Boutons Connexion
GUILayout.BeginHorizontal();
{
if (_allowConnection)
{
if (GUILayout.Button("<b>" + "Connecter 1" + "</b>", GUILayout.Width(100)))
{
InitiateConnectionToSFS2X_default();
}
}
}
GUILayout.EndHorizontal();
#endregion
GUILayout.Space(window_padding);//Space
GUILayout.Label(server_message_about_connexion);
GUILayout.Space(window_padding);//Space
GUILayout.Label(server_message_about_login);
}
GUILayout.EndScrollView();
}
#endregion
#endregion
#region About connection's phases
void InitiateConnectionToSFS2X_default()
{
#region Intance and Events
//Add to session SmartFox'Events
_smartFoxServer = SmartFoxInstance.Instance;
_smartFoxServer.AddEventListener(SFSEvent.CONNECTION, OnConnection);
_smartFoxServer.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
_smartFoxServer.AddEventListener(SFSEvent.LOGIN, OnLogin);
_smartFoxServer.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
_smartFoxServer.AddEventListener(SFSEvent.LOGOUT, OnLogout);
_smartFoxServer.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
#endregion
#region Connection To SFS2X Configuration
ConfigData cd = new ConfigData();
cd.Host = _IPServer;
cd.Port = _IPPort;
cd.Zone = _Zone;
//bluebox
cd.HttpPort = DataCenter.HttpIPPort;
cd.BlueBoxPollingRate = 750;
cd.UseBlueBox = _forceBlueBox;
_smartFoxServer.BitSwarm.ForceBlueBox(_forceBlueBox);
//Necessary for the web
if (Application.isWebPlayer || Application.isEditor)
{
if (_forceBlueBox)
Security.PrefetchSocketPolicy(_IPServer, DataCenter.HttpIPPort);
else
Security.PrefetchSocketPolicy(_IPServer, _IPPort);
}
#endregion
_smartFoxServer.Connect(cd);//Connect to your server instance
}
void EndConnectionPhase()
{
if (Application.loadedLevelName != _sceneName && Application.CanStreamedLevelBeLoaded(_sceneName))
{
Object.DontDestroyOnLoad(this);
Application.LoadLevel(_sceneName);
}
//Desactivate connection GUI
_allowConnection = false;
}
IEnumerator Ping(string val)
{
p = new Ping(val);
while (!p.isDone)
{
yield return new WaitForEndOfFrame();
}
}
#endregion
#region About Connection
//Alerts your console on the success of the connection
public void OnConnection(BaseEvent evt)
{
server_message_about_connexion = "Etat de la connexion ";
server_message_about_connexion += ((bool)evt.Params["success"]) ? "SUCCES" : "ECHEC";
server_message_about_connexion += ((bool)evt.Params["success"]) ? "" : " Erreur : <" + (string)evt.Params["errorMessage"] + ">)";
//Once connected (use event listeners), we want to Login to a SmartFoxServer Zone:
_smartFoxServer.Send(new LoginRequest("", "", _Zone));
}
//Will give you the reason why you were disconnected
void OnConnectionLost(BaseEvent evt)
{
server_message_about_connexion = "Connection lost" + " " + (string)evt.Params["Reason"];
OnLogout(null);
}
#endregion
#region About Login
//When you log in, this tells smart fox to initialize your buddy list
void OnLogin(BaseEvent evt)
{
try
{
if (evt.Params.Contains("success") && !(bool)evt.Params["success"])//if login phase failed ...
{
string loginErrorMessage = (string)evt.Params["errorMessage"];
server_message_about_login = "Login error : " + loginErrorMessage;
}
else
{
server_message_about_login = "Logged in successfully as " + ((User)evt.Params["user"]).Name;
//Once logged in to the zone (again, use event listeners), we can join a room:
_smartFoxServer.Send(new JoinRoomRequest(_Room));
}
}
catch (System.Exception ex)
{
Debug.Log("Exception handling login request: " + ex.Message + " " + ex.StackTrace);
}
}
#endregion
#region About Joining Space
//Runs when you join a room and notifies you in your chat log
void OnJoinRoom(BaseEvent evt)
{
Debug.Log("Room " + ((Room)evt.Params["room"]).Name + " joined successfully");
EndConnectionPhase();
}
#endregion
}
My Html5/Javascript code + SFS2X API
<script type="text/javascript">
<!--
var sfs = null;
function Connect( arg ){
var config = {};
config.host = "xx.xx.xx.xx";
config.port = 8888;
config.zone = "zintao";
config.debug = true;
// Create SmartFox client instance
sfs = new SmartFox(config);
// Add event listeners
sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);
sfs.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
sfs.addEventListener(SFS2X.SFSEvent.LOGIN_ERROR, onLoginError, this);
sfs.addEventListener(SFS2X.SFSEvent.LOGIN, onLogin, this);
sfs.addEventListener(SFS2X.SFSEvent.LOGOUT, onLogout, this);
sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError, this);
sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN, onRoomJoin, this);
sfs.connect();
//trace("SmartFox API version: " + sfs.version, true);
}
//------------------------------------
// SFS EVENT HANDLERS
//------------------------------------
function onConnection(event)
{
if (event.success)
{
trace("Server Connected to SmartFoxServer 2X!", true);
sfs.send(new SFS2X.Requests.System.LoginRequest("GuestName"));
}
else
{
var error = "Connection failed: " + (event.errorMessage ? event.errorMessage + " (code " + event.errorCode + ")" : "Is the server running at all?");
trace(error, true);
}
}
function onConnectionLost(event)
{
// Show disconnection reason
if (event.reason != SFS2X.Utils.ClientDisconnectionReason.MANUAL && event.reason != SFS2X.Utils.ClientDisconnectionReason.UNKNOWN)
{
trace("You have been disconnected");
var error = "You have been disconnected; reason is: " + event.reason;
showError(error);
}
else
trace("You have been disconnected; reason is: " + event.reason, true);
}
function onLoginError(event)
{
// Show error
var error = "Login error: " + event.errorMessage + " (code " + event.errorCode + ")";
showError(error);
}
function onLogin(event)
{
trace("Login successful!" +
"\n\tZone: " + event.zone +
"\n\tUser: " + event.user +
"\n\tData: " + event.data, true);
// Join lobby room
sfs.send(new SFS2X.Requests.System.JoinRoomRequest("test_de_connexion"));
}
function onLogout(event)
{
trace("Logout from zone " + event.zone + " performed!");
}
function onRoomJoinError(event)
{
trace("Room join error: " + event.errorMessage + " (code: " + event.errorCode + ")", true);
}
function onRoomJoin(event)
{
trace("Room joined: " + event.room);
}
function showError(text)
{
trace(text);
$("#errorLb").html("<b>ATTENTION</b><br/>" + text);
$("#errorLb").toggle();
}
function trace(txt, showAlert)
{
console.log(txt);
if (showAlert)
alert(txt);
}
-->
</script>