Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send/receive messages through a web socket on windows phone 8 using the class ClientWebSocket?

The web socket is written in javascript by my colleague. I managed to connect. First of all I have to log in on the application using a test account. I have to send the email and password through a json. I have installed the Json.Net packet using NuGet.

Some code that I found on my reaserch is this, but I do not understand how to send my data using that segment.

var buffer = new byte[1024];
var segment = new ArraySegment<byte>(buffer);
webSocket.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None);

Of course, I can use an object

User user=new User();
user.Email="[email protected]";
user.Password="pass";
string json = JsonConvert.SerializeObject(user);

But it will not be of any use because the method SendAsync accepts only byte type on segment.

All I want is to send that data, and if log in succeeds, I should receive other data (in Json format) about the user.

As a side note, I am quite new to web sockets, I used http protocols from ASP.NET WEB API 2.

like image 725
Liviu Sosu Avatar asked Jun 27 '14 10:06

Liviu Sosu


People also ask

How do I receive incoming messages on WebSocket?

The Message event takes place usually when the server sends some data. Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage function is fired.

How do I send a WebSocket message?

To send a message through the WebSocket connection you call the send() method on your WebSocket instance; passing in the data you want to transfer. socket. send(data); You can send both text and binary data through a WebSocket.

Can a WebSocket server send message to client?

With WebSockets: the server can send a message to the client without the client explicitly requesting something. the client and the server can talk to each other simultaneously. very little data overhead needs to be exchanged to send messages.

When using WebSocket send () How do you know?

The only way to know the client received the webSocket message for sure is to have the client send your own custom message back to the server to indicate you received it and for you to wait for that message on the server. That's the ONLY end-to-end test that is guaranteed.


1 Answers

I have no idea about Windows Phone 8, but by the code you pasted it seems similar to the regular .NET ClientWebSocket, so here you have some examples:

public static Task SendString(ClientWebSocket ws, String data, CancellationToken cancellation)
{
    var encoded = Encoding.UTF8.GetBytes(data);
    var buffer = new ArraySegment<Byte>(encoded, 0, encoded.Length);
    return ws.SendAsync(buffer, WebSocketMessageType.Text, true, cancellation);
}

public static async Task<String> ReadString(ClientWebSocket ws)
{
    ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]);

    WebSocketReceiveResult result = null;

    using (var ms = new MemoryStream())
    {
        do
        {
            result = await ws.ReceiveAsync(buffer, CancellationToken.None);
            ms.Write(buffer.Array, buffer.Offset, result.Count);
        }
        while (!result.EndOfMessage);

        ms.Seek(0, SeekOrigin.Begin);

        using (var reader = new StreamReader(ms, Encoding.UTF8))
            return reader.ReadToEnd();
    }
}

If something does not compile or exists in WP8, just find an equivalent.

like image 163
vtortola Avatar answered Oct 18 '22 00:10

vtortola