Manish Goyal
Manish Goyal

Reputation: 11

I want to implement client-side web socket connection in C#

Javascript code is working for this:

function testSocket()
{  
    var socket = io("http://localhost:3001/",{
      query:{Token:"YWRtaW5Ac3RsdGVjaC5pbjpzdGxAMTIzOjE2MTEzNDE3OTk1OTY="}, 
    });
    socket.on('message-processed',(event,callback)=>{
      writeToScreen(event);
    });
}

but I need to implement this in C#. I tried the code shown below, but it is not working:

var ws = new WebSocket("ws://localhost:3001/socket.io/Token=YWRtaW5Ac3RsdGVjaC5pbjpz=");
                
ws.Connect();
ws.OnMessage += (sender, e) =>
        Console.WriteLine("Laputa says: " + e.Data);

ws.Send("message-processed");
Console.ReadKey(true);

Upvotes: 1

Views: 1739

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26362

I am assuming that you are using socket.io. I'll reference the documentation here:

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

Needless to say websocket-sharp IS a websocket client implementation. Be that as it may, not all hope is lost:

I don't want to c/p the answers, so please find alternative client libraries here: Communicating with a socket.io server via c#

Upvotes: 1

Related Questions