我有一个对象,我从Unity上的客户端发送到我的服务器。服务器能够将数据重新组合成自然类型,并从属性中读取。
客户MessagaData
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
[Serializable]
public class MessagaData
{
public int type { get; set; }
public string dataString { get; set; }
public string User { get; set; }
public string Password { get; set; }
}
服务器MessagaData
using System;
namespace Server
{
[Serializable]
public class MessagaData
{
public int type { get; set; }
public string dataString { get; set; }
public string User { get; set; }
public string Password { get; set; }
}
}
当用户按下登录按钮时,它将首先创建一个MessagaData对象并为其分配一些属性。
public void sendLoginDetails()
{
if (string.IsNullOrEmpty (username.text))
return;
if (string.IsNullOrEmpty (password.text))
return;
MessagaData md = new MessagaData ();
md.type = 1;
md.User = username.text;
md.Password = password.text;
Client.Send (md);
}
然后调用静态函数:传递md。
static public void Send(MessagaData msgData)
{
if (singleton.client == null)
return;
string output = JsonConvert.SerializeObject(msgData);
byte[] data = new byte[1024];
NetworkStream ns = singleton.client.GetStream ();
data = Encoding.ASCII.GetBytes (output);
ns.Write (data, 0, data.Length);
ns.Flush ();
ReadThis ();
}
在发送代码对象后,调用此函数ReadMe,获取错误;
{"类型":1," dataString":空,"用户":空,"密码":空}
跟踪代码上的调试器
throw CreateJsonReaderException("After parsing a value an unexpected character was encountered: {0}. Line {1}, position {2}.", currentChar, _currentLineNumber, _currentLinePosition);
看来我从服务器收到的东西是我Json不能读的东西。
服务器:
private void myMethod(object obj)
{
int type = (int)obj;
MessagaData msgData = new MessagaData();
msgData.type = type;
string output = JsonConvert.SerializeObject(msgData);
byte[] data = new byte[1024];
NetworkStream ns = client.GetStream();
data = System.Text.Encoding.ASCII.GetBytes(output);
ns.Write(data, 0, data.Length);
ns.Flush();
}
为什么我的客户端在尝试构建MessagaData时崩溃了?
EDIT - 01/03/2016
1)您的ReadMe()方法是什么样的?
public static void ReadThis()
{
if (singleton.client == null)
return;
NetworkStream ns = singleton.client.GetStream ();
byte[] data = new byte[1024];
ns.Read (data, 0, data.Length);
string output = System.Text.Encoding.ASCII.GetString(data);
Debug.LogWarning (output);
MessagaData md = JsonConvert.DeserializeObject<MessagaData>(output); //i am the error
switch(md.type)
{
case 1:
Debug.LogWarning ("Success");
break;
case 2:
Debug.LogWarning ("Failed");
//Thread.Sleep (500);
break;
}
}
由于它是引发异常的服务器,您可以共享该代码吗?
在ReadMe()
方法
2)你能给出包含消息和回溯的异常的完整ToString()输出吗?知道Json.NET中的当前字符,行号和行位置以及堆栈跟踪会很有帮助。
这是在客户端上;
try {
MessagaData md = JsonConvert.DeserializeObject<MessagaData> (output);
switch(md.type)
{
case 1:
Debug.LogWarning ("Success");
break;
case 2:
Debug.LogWarning ("Failed");
//Thread.Sleep (500);
break;
}
} catch (System.Exception ex) {
Debug.Log (ex.ToString ());
}
调试显示:
> Newtonsoft.Json.JsonReaderException: After parsing a value an unexpected character was encountered:
UnityEngine.Debug:Log(Object)
Client:ReadThis() (at Assets/Scripts/Client.cs:73)
Client:Send(MessagaData) (at Assets/Scripts/Client.cs:45)
SendLoginInformation:sendLoginDetails() (at Assets/Scripts/SendLoginInformation.cs:26)
UnityEngine.EventSystems.EventSystem:Update()
3)服务器是否也使用unity3d,或者使用Microsoft .Net? 服务器是C#Microsoft .NET如果你愿意我可以压缩它并上传它,因为它是一个非常小的程序
致DBC。