-1

I'm using the following C# code to show real time data in my desktop application

            string strQuery = "AbcD";
            string socketURI = "https://mysocketio.com/";
            Dictionary<string, string> dictQS = new Dictionary<string, string>();
            dictQS.Add("token", strQuery);
            dictQS.Add("transport", "websocket");

            try
            {
                IO.Options options = new IO.Options() { AutoConnect = true, ForceNew = true, Path = "/socket/", Query = dictQS };

                var cSocket = IO.Socket(socketURI, options); //An item with the same key has already been added 
                cSocket.Connect();

                cSocket.On(Socket.EVENT_CONNECT, () =>
                {
                    Console.WriteLine("success");
                });

                cSocket.On("change", (data) => {

                    MessageBox.Show("change");

                });

                cSocket.On(Socket.EVENT_DISCONNECT, () =>
                {

                    Console.WriteLine("Disconnected");
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

But I got an error An item with the same key has already been added. Please provide a solution. Thanks.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Prince
  • 11
  • 3
  • can you copy the exception – Maytham Fahmi Jun 05 '20 at 09:43
  • What error? What library did you use? Don't just log an exception's message, log the full exception text. That includes the exact location where the error occurred, the stack trace and any inner exceptions. You can do that easily with `Console.WriteLine(ex)` – Panagiotis Kanavos Jun 05 '20 at 09:46
  • On the other hand `An item with the same key` means you tried to insert the same key into a dictionary twice. This has nothing to do with web sockets – Panagiotis Kanavos Jun 05 '20 at 09:48
  • Im using Quobject.SocketIoClientDotNet.Client library Here is the StackTrace mesaasge... at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at Quobject.EngineIoClientDotNet.Client.Socket.CreateTransport(String name) at Quobject.EngineIoClientDotNet.Client.Socket.Open() at Quobject.SocketIoClientDotNet.Client.Manager.Open(IOpenCallback fn) at Quobject.SocketIoClientDotNet.Client.Manager..ctor(Uri uri, Options opts) – Prince Jun 05 '20 at 09:48
  • I've done by [https://stackoverflow.com/questions/39112800/observing-incoming-websocket-messages-with-reactive-extensions?noredirect=1&lq=1](https://stackoverflow.com/questions/39112800/observing-incoming-websocket-messages-with-reactive-extensions?noredirect=1&lq=1). Thank you all. – Prince Jun 05 '20 at 13:37

1 Answers1

0

Maybe you should check if the key exists in dictionary?

     if (!dictQS.ContainsKey(yourKey))
         dictQS.Add(yourKey, yourValue);
     else
         dictQS[yourKey] = yourValue;