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

标签 c# sockets socket.io-1.0

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

我正在使用 SocketIoClientDotNet完成这项工作,这是我发现的唯一支持 socket.io 1.0 的选项(例如 SocketIO4Net.Client 不幸的是只支持 0.9)。该程序最初针对 .NET framework 4.0,我必须更改它以与 SocketIoClientDotNet 兼容。显然支持 .Net 3.5 和 4.5。

我的代码片段:

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 会导致其他依赖项中断并出现 错误 396 找不到类型或 namespace 名称“FooBar”(是否缺少 using 指令或程序集引用?) ,所以我也不能那样做。

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

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

最佳答案

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

关于c# - 将 Socket.io 1.0 客户端与 c# 程序集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27678628/

相关文章:

node.js - Socket.io 和 Express 设置

c# - 如何在 C# WinForms 中的 Label 上编写二次方程?

c# - 如何将 C# 类似字符串的变量传递给 sbyte* 参数?

c# - 如何使用 Moq 对必须在同一个 TransactionScope 中调用一组方法进行单元测试

C++ 套接字对不读/写父/子

node.js - Socket.io 向套接字发送消息时出错

node.js - socket.io 1.0.0^ : Using the default room vs a custom room for user specific communications

c# - 类调试,如何知道从哪里调用类

javascript - 在 node.js 网络服务器中写入结束后错误

sockets - 带套接字的 Google 语音 API v2