doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
721 stars 124 forks source link

Cannot connect to SocketAPIs? #297

Open Exilon24 opened 1 year ago

Exilon24 commented 1 year ago

I've written a bit of code to connect to my machine and just make sure it can connect to different URLs. So far, I've had no success using different SocketAPI's so I checked my localhost and I cannot connect to it either. Here is the code:

using SocketIOClient;

async Task startConnection()
{
    Console.WriteLine("Started connection attempt...");
    // string token = "IgnoreME";
    var client = new SocketIO("http://localhost:11000" //, new SocketIOOptions
    //{
        //Query = new List<KeyValuePair<string, string>>
    //{
        //new KeyValuePair<string, string>("emitTests", "True"),
        //new KeyValuePair<string, string>("token", token)
    //},
        //Reconnection = true
    //}
    );

    if (client != null)
    {
        Console.WriteLine("Created SocketIO object");
    }
    else
    {
        Console.WriteLine("Failed to create SocketIO object");
    }

    client.On("event", response =>
    {
        Console.WriteLine(response);
    });

    client.OnError += async (sender, e) =>
    {
        Console.WriteLine("There was an error");
        Console.WriteLine(e);
    };

    client.OnConnected += async (sender, e) =>
    {
        Console.WriteLine("Connected");
    };

    client.OnReconnectAttempt += async (sender, e) =>
    {
        Console.WriteLine("Attempting reconnect...");
    };

    await client.ConnectAsync();
    Console.WriteLine("Trying connect async...");
}

await startConnection();
Console.ReadKey(true);

Output (Debug ran for 40s):

Started connection attempt...
Created SocketIO object
Attempting reconnect...
Attempting reconnect...
Attempting reconnect...
Attempting reconnect...

Clearly its not connecting but I don't know why. Am I meant to be running a SocketIO server on my machine elsewhere? If so, how come I have the same problem with other API's? For example, I'm using the streamlabs API here:

using SocketIOClient;

async Task startConnection()
{
    Console.WriteLine("Started connection attempt...");
// Nothing malicious can be done with this token so I'm comfortable with showing it here
    string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbiI6IjYxRjExNEM3MTQ2RUIwOUJEQzg4QjM2M0M3OUFFOUMwQjlENUQ1ODdCMEE3RkM2RTFGQTgxQzdBMTM2RjlFRUQxREVFMUVDODlEOTg4QTdCNkFCNDc1NDRGOTI3NDMwRDM5RjQ4NjI0NDQyMTEyRUUwRTlEQkRCODgxNjFFOUU1QUVGQTcwNjcxQkNDQzBFMUFFOEM2ODUxNDU0QTA5QTMzRTU1QUI1NkJGQjM5MkYwNUFFRkQ3RTRBREMxODg2QURBMDY5MTkxQUYzRkFBOTQ5MkMzMEZFQjExQ0Q1REJCODEwNzcwQzk2QUYzODNFMUJGOTA4RjA3RkEiLCJyZWFkX29ubHkiOnRydWUsInByZXZlbnRfbWFzdGVyIjp0cnVlLCJ0d2l0Y2hfaWQiOiI1ODc0MzY1OTUifQ.jo55U9kPirljg1rWogbehWU6nqXXt2DroANeDab7OmQ";
    var client = new SocketIO($"https://sockets.streamlabs.com?token=${token}", new SocketIOOptions
    {
        Query = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("emitTests", "True"),
        new KeyValuePair<string, string>("token", token)
    },
        Reconnection = true
    });
    if (client != null)
    {
        Console.WriteLine("Created SocketIO object");
    }
    else
    {
        Console.WriteLine("Failed to create SocketIO object");
    }

    client.On("event", response =>
    {
        Console.WriteLine(response);
    });

    client.OnError += async (sender, e) =>
    {
        Console.WriteLine("There was an error");
        Console.WriteLine(e);
    };

    client.OnConnected += async (sender, e) =>
    {
        Console.WriteLine("Connected");
    };

    client.OnReconnectAttempt += async (sender, e) =>
    {
        Console.WriteLine("Attempting reconnect...");
    };

    await client.ConnectAsync();
    Console.WriteLine("Trying connect async...");
}

await startConnection();
Console.ReadKey(true);

And the output for this (Ran for 3m):

Started connection attempt...
Created SocketIO object
Trying connect async...

Note: I'm using the correct token which is the SocketAPI token taken from my account. However, the result is the same weather the token is included or not which leads me to believing that either my token is incorrect or there is a problem connecting in general

Have I made a dumb mistake or something? Because I cannot for the life of me tell what I'm doing wrong. Please help.

doghappy commented 1 year ago

you have tried to connect localhost, could you show me your server side(localhost socket.io server) code?

Exilon24 commented 1 year ago

I forgot to update this issue. I don't actually have server side code on localhost 😬. BUT, I have problems connecting to the API mentioned above. That seems to be a specific problem towards their API as I've managed to connect to other APIs. I'm only leaving this thread open so I can post a solution here when I get it. Sorry for the confusion.