doghappy / socket.io-client-csharp

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

Issue with connection #286

Closed arun5068 closed 2 years ago

arun5068 commented 2 years ago

I am trying to connect to https sever with latest version but could not get connected, The same server is working with the python socketio library .

Here is my C# Code

 var clientTest = new SocketIO("https://livestream.icicidirect.com", new SocketIOOptions
                {
                    Query = new List<KeyValuePair<string, string>>
                    {
                        new KeyValuePair<string, string>("user", UserID_Session),
                        new KeyValuePair<string, string>("token", Session_Token_64)
                    },
                    ReconnectionAttempts = 5,
                    EIO = 3,
                    Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
                }) ;
              var ExtraHeaders = new Dictionary<string, string>
                {
                    { "User-Agent", "abcdss" }
                };
clientTest.Options.ExtraHeaders = ExtraHeaders;

Here is python code

 self.hostname = 'https://livestream.icicidirect.com'
 auth = {"user": self.breeze.user_id, "token": self.breeze.session_key}
        self.sio.connect(self.hostname, headers={"User-Agent":"python-socketio[client]/socket123"}, auth=auth, transports="websocket", wait_timeout=3)

Tried with different EIO options also but no success. User-agent header is required otherwise server is not responding. Is there anyway to set User-Agent header or I have to modify original source code as suggested by https://stackoverflow.com/questions/22635663/setting-user-agent-http-header-in-clientwebsocket?

doghappy commented 2 years ago

could you try it?

 var clientTest = new SocketIO("https://livestream.icicidirect.com", new SocketIOOptions
 {
     Auth = new
    {
        user = UserID_Session,
        token = Session_Token_64
    },
    Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
}) ;
              var ExtraHeaders = new Dictionary<string, string>
                {
                    { "User-Agent", "abcdss" }
                };
clientTest.Options.ExtraHeaders = ExtraHeaders;
arun5068 commented 2 years ago

Well your library is a great work, I tried with .NETCore and .NETFramework both and here are my observations.

for .NETCore no problem but as my server is using Socket.io2x, i had to change the serialization to newtonsoft json to work properly.

for .NETFramework I have to use reflection to remove IsRequestRestricted from headerInfo hash table

here is the code for that

// use reflection to remove IsRequestRestricted from headerInfo hash table Assembly a = typeof(HttpWebRequest).Assembly; foreach (FieldInfo f in a.GetType("System.Net.HeaderInfoTable").GetFields(BindingFlags.NonPublic | BindingFlags.Static)) { if (f.Name == "HeaderHashTable") { Hashtable hashTable = f.GetValue(null) as Hashtable; foreach (string sKey in hashTable.Keys) {

                    object headerInfo = hashTable[sKey];
                    //Console.WriteLine(String.Format("{0}: {1}", sKey,

hashTable[sKey])); foreach (FieldInfo g in a.GetType("System.Net.HeaderInfo").GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) {

                        if (g.Name == "IsRequestRestricted")
                        {
                            bool b = (bool)g.GetValue(headerInfo);
                            if (b)
                            {
                      //          string name =

headerInfo.GetValue().Name; if (sKey == "User-Agent") { g.SetValue(headerInfo, false); //Console.WriteLine(sKey + "." + g.Name + " changed to false"); } }

                        }
                    }

                }
            }
        }

now it is working, though few issues are there related with disconnection but even in postman also i have seen disconnection problem.

Again great work man Cheers

https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free. www.avast.com https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Sun, Jul 17, 2022 at 3:06 PM HeroWong @.***> wrote:

could you try it?

var clientTest = new SocketIO("https://livestream.icicidirect.com", new SocketIOOptions { Auth = new { user = UserID_Session, token = Session_Token_64 }, Transport = SocketIOClient.Transport.TransportProtocol.WebSocket }) ; var ExtraHeaders = new Dictionary<string, string> { { "User-Agent", "abcdss" } };clientTest.Options.ExtraHeaders = ExtraHeaders;

— Reply to this email directly, view it on GitHub https://github.com/doghappy/socket.io-client-csharp/issues/286#issuecomment-1186460003, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWZKH5CBRMNS4EYW7KDFGSLVUPHZJANCNFSM53UBWFOA . You are receiving this because you authored the thread.Message ID: @.***>