Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

How to get real time data using socket.io to a Win Form using C#

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe you should check if the key exists in dictionary?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...