I am running server version 2.1.0, and my Unity client DLL is 1.0.3.
I added a coroutine 'Abuser()' to the GUI script which automatically disconnects and reconnects. If I run the script in the Unity editor, it gradually runs more and more slowly. If I build a standalone mac app with the Connection GUI, it runs for a little while and freezes up the player, which can then only be closed with Force Quit.
Edit: the editor will freeze up too... it just takes longer.
Below is my modified version of ConnectionGUI.cs from the Connector example:
Code: Select all
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Logging;
public class ConnectionGUI : MonoBehaviour
{
//----------------------------------------------------------
// Setup variables
//----------------------------------------------------------
public string serverName = "127.0.0.1";
public int serverPort = 9933;
public LogLevel logLevel = LogLevel.DEBUG;
public GUISkin sfsSkin;
private string statusMessage = "";
private SmartFox smartFox;
private List<string> logMessages = new List<string>();
private Vector2 logScrollPosition;
//----------------------------------------------------------
IEnumerator Abuser()
{
while(true)
{
if(smartFox.IsConnected)
smartFox.Disconnect();
else
smartFox.Connect(serverName, serverPort);
yield return new WaitForEndOfFrame();
}
}
//----------------------------------------------------------
// Called when program starts
//----------------------------------------------------------
void Start()
{
// In a webplayer (or editor in webplayer mode) we need to setup security policy negotiation with the server first
if (Application.isWebPlayer /*|| Application.isEditor*/) {
if (!Security.PrefetchSocketPolicy(serverName, serverPort, 500)) {
Debug.LogError("Security Exception. Policy file load failed!");
}
}
// Register for basic callbacks
smartFox = new SmartFox(true);
smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
// Also register for all debug messages from the API at the given log level
smartFox.AddLogListener(logLevel, OnDebugMessage);
statusMessage = "Not connected";
logMessages.Add("Log Messages:");
StartCoroutine("Abuser");
}
//----------------------------------------------------------
// As Unity is not thread safe, we process the queued up callbacks every physics tick
//----------------------------------------------------------
void FixedUpdate() {
smartFox.ProcessEvents();
}
//----------------------------------------------------------
// Draw GUI every frame
//----------------------------------------------------------
void OnGUI() {
if (smartFox == null) return;
GUI.skin = sfsSkin;
// Lets just quickly set up some GUI layout variables
float panelWidth = 400;
float panelHeight = 300;
float panelPosX = Screen.width/2 - panelWidth/2;
float panelPosY = Screen.height/2 - panelHeight/2;
// Draw the box
GUILayout.BeginArea(new Rect(panelPosX, panelPosY, panelWidth, panelHeight));
GUILayout.Box ("Connector Example", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
GUILayout.BeginVertical();
GUILayout.BeginArea(new Rect(20, 25, panelWidth-40, panelHeight-100), GUI.skin.customStyles[0]);
// Show server log inside
logScrollPosition = GUILayout.BeginScrollView(logScrollPosition, false, false);
foreach (string message in logMessages) {
GUILayout.Label(message);
}
GUILayout.EndScrollView();
GUILayout.EndArea ();
GUILayout.BeginArea(new Rect(20, panelHeight-70, panelWidth-40, 80));
// Display client status
GUIStyle centeredLabelStyle = new GUIStyle(GUI.skin.label);
centeredLabelStyle.alignment = TextAnchor.MiddleCenter;
GUILayout.Label("Client Status: " + statusMessage, centeredLabelStyle);
// Center button
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (!smartFox.IsConnected) {
// Connect button
if (GUILayout.Button("CONNECT")) {
smartFox.Connect(serverName, serverPort); ;
}
}
else {
// Show disconnect button
if (GUILayout.Button("DISCONNECT")) {
smartFox.Disconnect();
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea ();
GUILayout.EndVertical();
GUILayout.EndArea ();
}
//----------------------------------------------------------
// Handle connection response from server
//----------------------------------------------------------
public void OnConnection(BaseEvent evt) {
bool success = (bool)evt.Params["success"];
if (success) {
statusMessage = "Connection succesful!";
} else {
statusMessage = "Can't connect to server!";
}
}
public void OnConnectionLost(BaseEvent evt) {
statusMessage = "Connection was lost, Reason: " + (string)evt.Params["reason"];
}
//----------------------------------------------------------
// Show the debug messages both in window as well as console log
//----------------------------------------------------------
public void OnDebugMessage(BaseEvent evt) {
string message = (string)evt.Params["message"];
logMessages.Add(message);
Debug.Log("[SFS DEBUG] " + message);
}
//----------------------------------------------------------
// Disconnect from the socket when shutting down the game
//----------------------------------------------------------
public void OnApplicationQuit() {
smartFox.Disconnect();
}
}