我在Unity 5中使用C#。我使用.NET 4.5构建的应用程序移植了一些TCP代码,主要依据:
Asynchronous Socket Communication
我无法在服务器应用程序中接收数据时调用任何方法。
我在一个事件上收到TCP通信:
public void OnReceivedData(IAsyncResult ar)
{
SocketChatClient client = (SocketChatClient)ar.AsyncState;
byte[] aryRet = client.GetRecievedData(ar);
string aryRetString = System.Text.Encoding.ASCII.GetString(aryRet);
ParseCommand(aryRetString);
if (aryRet.Length < 1)
{
client.Sock.Close();
tcpClients.Remove(client);
return;
}
client.SetupReceiveCallback(this);
}
ParseCommand()非常简单(用于测试):
private void ParseCommand(string cmdToParse)
{
string[] splitInput = cmdToParse.Split(',');
testValue++;
Debug.Log(testValue.ToString());
int playerStation = 0;
}
当我使用Debug.Log()从ParseCommand向控制台输出测试值类变量时,每次调用ParseCommand时它都会正确递增。但是,如果我在ParseCommand之外连续输出testValue,它永远不会递增。此外,如果我在ParseCommand中调用一个方法,该方法不会触发,并且它的行为就好像它已经异常但没有抛出异常 - 即,该方法在该行停止并且调用方法不会再次触发。 / p>