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.
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);
}
}
Any advice is appreciated.