我想弄清楚这个问题,
我想使用ArraySegment
传输Json对象我的JSON数据是 以这种格式
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
我使用this工具创建了像
这样的指定类public class Employee
{
public string firstName { get; set; }
public string lastName { get; set; }
}
public class RootObject
{
public List<Employee> employees { get; set; }
}
现在我将数据插入像这样的对象
var objectToSerialize = new RootObject();
objectToSerialize.items = new List<Employee>
{
new Item { firstName="abc", lastName="xyz"},
new Item { firstName="pqr", lastName="stu"}
};
但现在我遇到了问题,我不知道如何使用它 websocket
通过这个
ArraySegment<byte> max = new ArraySegment<byte>(, , );
await webSocket.SendAsync(max, WebSocketMessageType.Text, true, CancellationToken.None);
答案 0 :(得分:4)
您需要首先将objectToSerialize序列化为字节缓冲区
var data = JsonConvert.SerializeObject(objectToSerialize);
var encoded = Encoding.UTF8.GetBytes(data);
var buffer = new ArraySegment<Byte>(encoded, 0, encoded.Length);
await webSocket.SendAsync.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);