I am trying to create a bot which that listen to an WebSocket URL with C# and ASP.Net Core 2.2.
But I’m new in WebSocket area.
I read Microsoft documentation about WebSocket and ClientWebSocket to solve my problem. but there are not help to me.
How my app can listen to an external WebSocket URL and react to incoming messages ?
I tried below code but get "Cannot create an instance of the abstract class or interface 'WebSocket'." error.
using (var ws = new WebSocket("wss://ws.example.com"))
{
ws.OnMessage += (sender, e) =>
Console.WriteLine("Message received" + e.Data);
ws.OnError += (sender, e) =>
Console.WriteLine("Error: " + e.Message);
ws.Connect();
Console.ReadKey(true);
}
WebSocket
is an abstract class , you can use ClientWebSocket`.