Socket.io 1.0 客户端与 c# 程序集成

本文关键字:程序 程序集 集成 客户端 io Socket | 更新日期: 2023-09-27 18:35:24

我是套接字新手,我一直在尝试在我的 C# 程序中集成 Socket.io 1.0 客户端,但没有取得多大成功。

我正在使用SocketIoClientDotNet来完成这项工作,这是我找到的唯一支持 socket.io 1.0的选项(例如,不幸的是,SocketIO4Net.Client仅支持0.9)。该程序最初针对.NET Framework 4.0,我必须对其进行更改才能与显然支持.Net 3.5和4.5的SocketIoClientDotNet兼容。

我的代码片段:

using SteamKit2;
using SteamTrade;
using SteamTrade.TradeWebAPI;
using SteamTrade.TradeOffer;
using System;
using System.IO;
using System.Net;
using System.Timers;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using MySql.Data.MySqlClient;
using Quobject.SocketIoClientDotNet.Client;
using Quobject.EngineIoClientDotNet.Client.Transports;
using Quobject.EngineIoClientDotNet.ComponentEmitter;
using Quobject.EngineIoClientDotNet.Modules;
using Quobject.EngineIoClientDotNet.Parser;
using Socket = Quobject.SocketIoClientDotNet.Client.Socket;
namespace Foo
{
    public class Bar
    {
        public override void fooBar()
        {
            // just to test, didn't go any further because of errors
            var socket = IO.Socket("http://localhost");
            socket.On(Socket.EVENT_CONNECT, () =>
            {
                socket.Emit("hi");
                socket.On("hi", (data) =>
                {
                    Console.WriteLine(data);
                    socket.Disconnect();
                });
            });
            // more code...
        }
        // more code...
    }
}

这就是问题开始的地方:

SocketIoClientDotNet 添加了对 System.Threading.Tasks.NET35 的引用。

»» 以 .Net Framework 4.0 或 4.5 为目标而不删除引用会导致错误:

Error   17  The type 'System.Threading.Tasks.Task<TResult>' exists in both 'c:'Program Files (x86)'Reference Assemblies'Microsoft'Framework'.NETFramework'v4.0'mscorlib.dll' and 'c:'MyApp'trunk'packages'System.Threading.Tasks.Unofficial.3.1'lib'net35'System.Threading.Tasks.NET35.dll'

»» 将目标框架更改为 3.5 会导致其他依赖项中断Error 396 The type or namespace name 'FooBar' could not be found (are you missing a using directive or an assembly reference?),所以我也不能那样做。

»» 删除对System.Threading.Tasks.NET35的引用会导致System.IO.FileNotFoundException异常,Quobject.SocketIoClientDotNet.Client.IO

基本上我被困在这里,需要指导。有关如何在这里 Socket.io 1.0工作的任何帮助和建议将不胜感激。谢谢!

Socket.io 1.0 客户端与 c# 程序集成

显然,这个问题是由于在面向.Net 4.0时安装了SocketIoClientDotNet,这错误地需要System.Threading.Tasks.NET35。以.Net 4.5为目标,然后才重新安装SocketIoClientDotNet解决了这个问题。