0

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

DarioN1
  • 2,460
  • 7
  • 32
  • 67

1 Answers1

1

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.

Community
  • 1
  • 1
Dypso
  • 563
  • 1
  • 5
  • 15
  • Does websocket-shap support additional headers ? – DarioN1 Dec 28 '16 at 12:49
  • Thanks for you precius help. Do you know if websocket-shap support additional headers ? I need to add User-Agent as header, otherwise the websocket doesn't accept connections... – DarioN1 Dec 28 '16 at 12:50
  • 1
    Hello, you could not change the user-agent but if you grab the code of the library, you could make it as a poperty or hardcode your user-agent (for this you have to change the code in this class : https://github.com/sta/websocket-sharp/blob/master/websocket-sharp/HttpRequest.cs) – Dypso Dec 28 '16 at 18:40