4

I am trying to figure out this problem,

I want to transfer Json object using ArraySegment

my JSON Data is in this format

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

I have used this tool to create specified class like

public class Employee
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}

public class RootObject
{
    public List<Employee> employees { get; set; }
}

now I am inserting data into object like this

var objectToSerialize = new RootObject();
objectToSerialize.items = new List<Employee> 
          {
             new Item { firstName="abc", lastName="xyz"},
             new Item { firstName="pqr", lastName="stu"}
          };

but now I am facing problem, I don't know how to send it using websocket

through this

ArraySegment<byte> max = new ArraySegment<byte>(, , );
await webSocket.SendAsync(max, WebSocketMessageType.Text, true, CancellationToken.None);

1 Answer 1

6

You need to serialize the objectToSerialize into a buffer of bytes first

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.