Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am trying my hand with Websockets to get data out from a particular URI.
From what I understand is that there are no Web API endpoints, just a single URI with various forms of JSON data coming through from the server side.
I was wondering if there is a simpler way of getting data from that endpoint beyond what appears to be raw websocket connections in my console application.
The data collected will be logged for processing later.

What I have tried:

The examples I have seen so far and am now testing is on the low level socket.

C#
clientWebSocket = new ClientWebSocket();
await clientWebSocket.ConnectAsync(new Uri(serverSetting.KSimNetAPIServerUrl), stoppingToken);

                while (!stoppingToken.IsCancellationRequested)
                {
                    var buffer = new ArraySegment<byte>(new byte[2048]);
                    WebSocketReceiveResult result;
                    String json;

                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        do
                        {
                            result = await clientWebSocket.ReceiveAsync(buffer, stoppingToken);
                            ms.Write(buffer.Array, buffer.Offset, result.Count);
                        } while (!result.EndOfMessage);

                        if (result.MessageType == WebSocketMessageType.Close)
                            break;

                        ms.Seek(0, System.IO.SeekOrigin.Begin);

                        json = Encoding.UTF8.GetString(ms.ToArray());
                        _logger.LogInformation("Received: {0}", json);
                    }
                    // TODO: Processing of the JSON data
                }


Any advice is appreciated.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900