I have 2 projects, server and client.
The server works without problems, it loads and in debug it says it’s running and message handlers are registered. also I’ve checked the work in background to true (same for client). But the problem is with the client, When it tried to connect it seems to fail so, the on connect event is never fired, and some errors show in debug console:
And the server never gets the connection at all, it doesn’t seem to write anything in debug window (developer level).
I’ve tried building the server just to make sure it’s not the editor stopping but same problem.
Here’s the full scene, if needed I can provide the files.
Projects Content… Long one.
1-Server:
Starts in a scene with a single empty object having this script:
public class Servermanager : MonoBehaviour {
void Awake () {
DontDestroyOnLoad (this);
MyServer.Instance.StartServer ();
}
}
The MyServer is the class I use to manage server, most of the code is taken from Unity’s NetworkManager:
public class MyServer {
static readonly MyServer _instance;
public static MyServer Instance{ get { return _instance; } }
public string ServerIP="localhost";
public int ServerPort=9999;
public float MaxDelay=20f;
//Message readers.
static AddPlayerMessage AddPlayerMessage = new AddPlayerMessage();
static RemovePlayerMessage RemovePlayerMessage = new RemovePlayerMessage();
static ErrorMessage ErrorMessage = new ErrorMessage();
public string OnlineScene="MainMap";
public string OfflineScene="OfflineScene";
static MyServer(){
_instance = new MyServer ();
LogFilter.currentLogLevel = (int)LogFilter.FilterLevel.Developer;
Application.runInBackground = true;
Network.InitializeSecurity ();
NetworkCRC.scriptCRCCheck = false; //tried using true too.
ConnectionConfig config = new ConnectionConfig ();
config.AddChannel (QosType.ReliableSequenced);
config.AddChannel (QosType.Unreliable);
NetworkServer.Configure (config, 2000);
}
public void StartServer(){
RegisterServerMessages ();
if (NetworkServer.Listen (ServerIP, ServerPort)) {
SceneManager.LoadSceneAsync (OnlineScene);
NetworkServer.SpawnObjects ();
}
}
public void StopServer(){
if (!NetworkServer.active)
return;
NetworkServer.Shutdown();
SceneManager.LoadScene (OfflineScene);
}
void RegisterServerMessages(){
NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnServerDisconnect);
NetworkServer.RegisterHandler(MsgType.Ready, OnServerReadyMessage);
NetworkServer.RegisterHandler(MsgType.AddPlayer, OnServerAddPlayerMessage);
NetworkServer.RegisterHandler(MsgType.RemovePlayer, OnServerRemovePlayerMessage);
NetworkServer.RegisterHandler(MsgType.Error, OnServerError);
}
void OnServerConnect(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerConnectInternal"); }
netMsg.conn.SetMaxDelay(MaxDelay);
}
void OnServerDisconnect(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerDisconnectInternal"); }
}
void OnServerAddPlayerMessage(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerAddPlayerMessageInternal"); }
netMsg.ReadMessage(AddPlayerMessage);
if (AddPlayerMessage.msgSize != 0)
{
var reader = new NetworkReader(AddPlayerMessage.msgData);
//OnServerAddPlayer(netMsg.conn, AddPlayerMessage.playerControllerId, reader);
}
else
{
//send error message.
}
}
void OnServerRemovePlayerMessage(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerRemovePlayerMessageInternal"); }
netMsg.ReadMessage(RemovePlayerMessage);
}
void OnServerError(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerErrorInternal"); }
netMsg.ReadMessage(ErrorMessage);
}
//NOT USED.
void OnServerReadyMessage(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerReadyMessageInternal"); }
}
}
pretty simple code, just to make the server start, it does start and debug does log that server is started with all the messages registered, and the online scene loads.
That’s the whole server project.
2-Client:
First scene is called PatchScene that will be used to update game components if required. it have single button to connect to server once all checks are done, when clicked the Launch method is called.
The simple patch mocking script is here:
public class Patcher : MonoBehaviour {
bool done = false;
public void Awake(){
//Find client manager, if exist destroy object.
foreach (var manager in FindObjectsOfType<ClientManager>()) {
if (manager != this) {
Debug.Log ("Destroying manager: " + manager);
Destroy (manager);
}
}
Debug.Log ("Starting to patch");
StartCoroutine ("Patch");
}
public void Launch(){
if (done)
SceneManager.LoadScene ("LoginScene");
else
Debug.Log ("Still patching");
}
IEnumerator Patch(){
yield return new WaitForSeconds (5f);
Debug.Log ("Done Patching");
done = true;
}
}
Finally the LoginScene have the following: camera, light, event system, and client manager object.
client manager have this code attached:
public class ClientManager : MonoBehaviour {
public void Login(){
string username = GameObject.Find ("UsernameField").GetComponent<InputField> ().text;
string password = GameObject.Find ("PasswordField").GetComponent<InputField> ().text;
GameObject.Find ("LoginButton").GetComponentInChildren<Text> ().text = username + ":" + password;
Debug.Log ("Logged in Successfully");
StartCoroutine ("LoginWait");
}
IEnumerator LoginWait(){
Debug.Log ("Loading Characters");
yield return new WaitForSeconds (5f);
Debug.Log ("Loaded Characters... moving to select scene.");
//SceneManager.LoadScene ("SelectCharacterScene");
}
}
And finally the MyClient class, similar to MyServer that manages the network connection of clients:
public class MyClient {
static MyClient _instance;
public static MyClient Instance{ get { return _instance; } }
static NetworkClient client;
static float maxDelay=20f;
public static string serverIP = "localhost";
public static int serverPort =9999;
public static string username;
public static string password;
static MyClient(){
_instance = new MyClient ();
LogFilter.currentLogLevel = (int)LogFilter.FilterLevel.Developer;
Application.runInBackground = true;
NetworkCRC.scriptCRCCheck = false;//tried with true
client = new NetworkClient ();
ConnectionConfig config = new ConnectionConfig ();
config.AddChannel (QosType.ReliableSequenced);
config.AddChannel (QosType.Unreliable);
client.Configure (config, 2000);
RegisterClientMessages ();
}
static void RegisterClientMessages()
{
client.RegisterHandler(MsgType.Connect, OnClientConnectInternal);
client.RegisterHandler(MsgType.Disconnect, OnClientDisconnectInternal);
client.RegisterHandler(MsgType.NotReady, OnClientNotReadyMessageInternal);
client.RegisterHandler(MsgType.Error, OnClientErrorInternal);
client.RegisterHandler(MsgType.Scene, OnClientSceneInternal);
}
public bool IsConnected(){
return client.isConnected;
}
public bool Connect(){
client.Connect (serverIP, serverPort);
return client.isConnected;
}
static void OnClientConnectInternal(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientConnectInternal"); }
netMsg.conn.SetMaxDelay(maxDelay);
LoadLoginGUI ();
}
static void LoadLoginGUI(){
Canvas canvas = Resources.Load<Canvas> ("GUI/LoginCanvas");//Pre-designed canvas for login.
GameObject.Instantiate<Canvas>(canvas);
GameObject.Find ("LoginButton").GetComponent<Button> ().onClick.AddListener (
GameObject.Find("ClientManager").GetComponent<ClientManager>().Login
);
}
static void OnClientDisconnectInternal(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientDisconnectInternal"); }
SceneManager.LoadScene("PatchScene");
}
static void OnClientNotReadyMessageInternal(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientNotReadyMessageInternal"); }
}
static void OnClientErrorInternal(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientErrorInternal"); }
}
static void OnClientSceneInternal(NetworkMessage netMsg)
{
if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientSceneInternal"); }
}
}
Anyone can tell me what’s the problem? and how would I solve it???