Connecting to websocket using C# (I can connect using JavaScript, but C# gives Status code 200 error)

后端 未结 6 1754
情书的邮戳
情书的邮戳 2021-01-30 07:38

I am new in the area of websocket.

I can connect to websocket server using JavaScript using this code:

var webSocket = new WebSocket(url);
6条回答
  •  既然无缘
    2021-01-30 08:10

    Since WebsocketSharp is not .NET Core compatible I suggest using this library. Here's some sample code

    static async Task Main(string[] args)
    {
        var url = new Uri("wss://echo.websocket.org");
        var exitEvent = new ManualResetEvent(false);
    
        using (var client = new WebsocketClient(url))
        {
            client.MessageReceived.Subscribe(msg => Console.WriteLine($"Message: {msg}"));
            await client.Start();
    
            await client.Send("Echo");
    
            exitEvent.WaitOne();
        }
    
        Console.ReadLine();
    }
    

    Be sure to use ManualResetEvent. Otherwise it doesn't work.

提交回复
热议问题