I have made a framework .dll that encapsulates a lot of networking stuff. I use it outside of Unity without any issues at all. However, adding it to Unity (inside a Plugins folder) works, but only until I try sending messages back/forth.
The gist of it is that the Login script runs, handling events from the ServerManager that it needs (connections/messages). The messages are a custom class from my framework. Now, the connections work great, as I am connected. I also get the first message from the server that it will now accept messages, so it correctly turns the login button on. However, after clicking the login button (which calls the LoginClick), I do not get anything back from the server. No message is returned, and no message from the server can come through anymore (including the previous working “accepting messages”).
The login function correctly works, as verified by debugging the received message on the server side. It correctly finds the information in our database, and writes back the correct message.
The odd thing about this, it all works when I made a test Windows Form application. Using the same ServerManager approach, I made a WinForm with the same design and textboxes, and even the login call on a button click. It works, and I even get the message back (“AccountLogin”) from the server properly.
So, it can’t really be an issue with my framework/dll, could it be? I mean the whole thing works, then just breaks after calling the login function on a Unity button click. The exact same scenario works in WinForms. Unity even gets all messages I send from the server until I click that button to try logging in.
ServerManager.cs:
using UnityEngine;
using System.Collections;
using RiptideShared;
using RiptideShared.Messaging;
using System.Collections.Generic;
public static class ServerManager
{
private static RiptideClient _client = null;
public delegate void OnConnectionChangedEventHandler(RiptideClient Sender, eConnectionStatus ConnectionStatus);
public static event OnConnectionChangedEventHandler OnConnectionChanged;
public delegate void OnMessageReceivedEventHandler(RiptideClient Sender, Message Message);
public static event OnMessageReceivedEventHandler OnMessageReceived;
public static bool ClientConnected
{
get
{
if(_client.ConnectionStatus == RiptideShared.eConnectionStatus.Connected)
return true;
else
return false;
}
}
public static RiptideClient Client { get { return _client; } }
public static void Start()
{
if (_client == null)
{
_client = new RiptideShared.RiptideClient();
_client.OnConnectionChanged += HandleOnConnectionChanged;
_client.OnMessageReceived += HandleOnMessageReceived;
}
_client.Connect ();
}
static void HandleOnConnectionChanged (RiptideClient Sender, eConnectionStatus ConnectionStatus)
{
Debug.Log(ConnectionStatus.ToString());
if (OnConnectionChanged != null)
OnConnectionChanged(Sender, ConnectionStatus);
}
static void HandleOnMessageReceived (RiptideClient Sender, Message Message)
{
Debug.Log(Message.MessageType);
if (OnMessageReceived != null)
OnMessageReceived(Sender, Message);
}
public static void Login(string Username, string Password)
{
_client.WriteTo(new Message(null, Message.MessageTypes.AccountLogin, new List<object>() { Username, Password }));
}
}
Login.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using RiptideShared.Messaging;
using RiptideShared;
using System.Collections.Generic;
public class Login : MonoBehaviour {
Text _connectionStatusLabel;
InputField _emailInputField;
InputField _passwordInputField;
Button _loginButton;
bool _serverAcceptingMessages = false;
RiptideShared.eConnectionStatus connectionStatus = eConnectionStatus.NotConnected;
// Use this for initialization
void Start () {
//get the variables filled up
_connectionStatusLabel = (Text)GameObject.Find("ConnectionStatusLabel").GetComponent(typeof(Text));
_emailInputField = (InputField)GameObject.Find("EmailInput").GetComponent(typeof(InputField));
_passwordInputField = (InputField)GameObject.Find("PasswordInput").GetComponent(typeof(InputField));
_loginButton = (Button)GameObject.Find("LoginButton").GetComponent(typeof(Button));
//start the server manager
ServerManager.OnConnectionChanged += ServerManager_OnConnectionChanged;
ServerManager.OnMessageReceived += ServerManager_OnMessageReceived;
ServerManager.Start();
}
void ServerManager_OnMessageReceived(RiptideClient Sender, Message Message)
{
switch(Message.MessageType)
{
case Message.MessageTypes.ServerAcceptingMessages:
_serverAcceptingMessages = true;
break;
case Message.MessageTypes.AccountLogin:
break;
}
}
void ServerManager_OnConnectionChanged(RiptideClient Sender, eConnectionStatus ConnectionStatus)
{
connectionStatus = ConnectionStatus;
}
// Update is called once per frame
void Update () {
_connectionStatusLabel.text = "Connection Status: " + connectionStatus.ToString();
if ((!_loginButton.interactable) && (connectionStatus == eConnectionStatus.Connected) && (_serverAcceptingMessages))
_loginButton.interactable = true;
else if (connectionStatus != eConnectionStatus.Connected)
_loginButton.interactable = false;
}
public void LoginClick()
{
ServerManager.Login(_emailInputField.text, _passwordInputField.text);
}
}