socket.io-client-csharp
socket.io-client-csharp copied to clipboard
Doesn't Connect, Stuck at ConnectAsync
The following code doesn't work, however the equivalent in JS works with no problem. It's a v2.x server and I'm using EIO 3. The client simple doesn't connect. C# code:
static async Task Main(string[] args)
{
var sio = new SocketIO("wss://trade.csgoempire.com/trade", new SocketIOOptions()
{
EIO = 3,
Transport = SocketIOClient.Transport.TransportProtocol.WebSocket,
Path = "/s/",
ExtraHeaders = new Dictionary<string, string>() { {"User-agent", "Test API BOT"} }
});
sio.On("connect", data =>
Console.WriteLine("connected" + data)
);
sio.On("new_item", data =>
Console.WriteLine(data)
);
await sio.ConnectAsync();
}
Equivalent in JS:
async function activateSocket() {
//define socket
const socket = io(
"wss://trade.csgoempire.com/trade", {
transports: ["websocket"],
path: "/s/",
extraHeaders: { 'User-agent': `Test API Bot` }
}
);
socket.on('connect', async() => {
console.log(`Connected to websocket`);
//log new items
socket.on('new_item', (data) => console.log(data));
});
};
@DanialPhanToM
I've made it working for me with work around
_socketIo.ConnectAsync();
var connectionAttemptsCount = (int)_socketIo.Options.ConnectionTimeout.TotalSeconds * 20;
for (var counter = 0; counter < connectionAttemptsCount; counter++)
{
if (connectionAttemptsCount == counter)
{
OnError?.Invoke(this, "Could not establish socket connection.");
return false;
}
if (_socketIo.Connected)
{
return true;
}
Thread.Sleep(50);
}
I got some data
Hi! how did you solve it?
Hi! how did you solve it?
Hey there;
I suppose you mean the whole thing and not the specific socket that I was using. I actually don't know what the problem was, I rewrote basically the same code on .NET 6.0 and it just worked with no problem.
and if you meant the socket that I was using, it turned out they had some changes to how it's meant to be used (needed emiting some data)
also you can add Reconnection = false
in your options to help with debugging and see what's goin wrong
EDIT: so if you look at #300 there seems to be a problem with .Net 4.0 when you add ExtraHeaders
. So that could be your problem as well.