如何使用 C# 将 socket.io 获取实时数据到 Win 窗体



我使用以下 C# 代码在我的桌面应用程序中显示实时数据

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);
}

但是我An item with the same key has already been added收到错误.请提供解决方案。 谢谢。

也许您应该检查字典中是否存在该键?

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

最新更新