简体   繁体   中英

Connecting to web socket server from C#

i am having an actual melt down about connecting too a web socket in c#, i have written a client web socket in JavaScript that works fine but i cannot for the life of me find any decent information on how to do it in C#

var socket = new WebSocket('wss://www.bitmex.com/realtime?subscribe=instrument:XBTUSD');

      socket.onopen = function(data)
      { 
        socket.onmessage = function(data)
        {      
          if(text.includes("instrument"))
          {           
            console.log(text);
          }
        }
      }

this is my javascript code, is anyone able to point me into the direction of something this simple but for C#?? everything i find only just looks so over complicated. cheers in advance!

Figured it out, for anyone wanting to know

in your main:
Task.Run(async () => await Connect());

public async Task Connect()
{
    var url = new Uri("wss://www.bitmex.com/realtime?subscribe=instrument");
    var exitEvent = new ManualResetEvent(false);

    using (var client = new WebsocketClient(url))
    {
          client.MessageReceived.Subscribe(msg => Recieve(msg.ToString()));
          await client.Start();
          exitEvent.WaitOne();
    }
}

public void Recieve(string txt)
{
     Console.WriteLine(txt);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM