Search code examples
c#websocketclientwebsocket

Scrape data from WebSocket with C# .Net


I need to scrape data from a websocket "just one time".

I mean that I don't need to get continuosly data from the source but I need to get the data one time and exit from the elaboration.

I don't need Async task, I just need to use it like a simple API.

I'm struggling with web search but I didn't find any solution.

Thanks to support


Solution

  • I think your are struggling to find such code as websocket is almost a bi-directional protocol, not really for a RPC style. But to answer your need, you could certainly use a library such as websocket-sharp . Your code to send a command text and receive back a return message would look like this :

    using System;
    using WebSocketSharp;
    
    namespace Example
    {
      public class Program
      {
        public static void Main (string[] args)
        {
          using (var ws = new WebSocket ("ws://YOUR_URI_HERE")) {
            ws.OnMessage += (sender, e) =>
                Console.WriteLine ("It's ok I got my answer back : " + e.Data);
    
            ws.Connect ();
            ws.Send ("COMMAND FOR THE SERVER");
            Console.ReadKey (true);
          }
        }
      }
    }
    

    Hope that code will help you to get started.