【问题标题】:How to connect SocketIO with C# client如何将 SocketIO 与 C# 客户端连接
【发布时间】:2020-01-14 20:56:57
【问题描述】:

C# 我正在为 Milestone VMS 开发 MIP 插件。 我在使用 C# 连接到 SocketIO 时遇到了一个问题。 我尝试使用 TcpClient、Socket、ClientWebSocket 连接到 SocketIO


    TcpClient tcpClient = new TcpClient();

    tcpClient.Connect("127.0.0.1, 3001);

我也尝试与 ClientWebSocket 连接,但服务器端再次没有反应。

    using (var client = new ClientWebSocket())
                {
                    // await client.ConnectAsync(new Uri("ws://192.168.100.25:8090/?token="),timeout.Token);
                    await client.ConnectAsync(new Uri(LOCAL_PATH), timeout.Token);
                    var buffer = new ArraySegment<byte>(new byte[1000]);

                    var result = await client.ReceiveAsync(buffer, timeout.Token);
                }

谁能提供一些可以作为 SocketIO 客户端的库?

URI 的语法如下: http://127.0.0.1:3001?token=xxx

【问题讨论】:

  • 您的服务器正在运行吗?似乎您正在尝试连接到同一台机器 127.0.0.1
  • 是的,我的服务器正在运行。是的,我正在尝试连接到在我的计算机中运行的服务器。但是我在另一台 PC 上的服务器上尝试过同样的事情。结果是一样的。

标签: c# .net socket.io tcpclient milestone


【解决方案1】:

这是我使用的经过测试的代码,假设您的服务器正在运行,您需要传递 IP 或主机名和端口号,然后输入您要发送的有效负载或消息:

private bool ConnectAndSendMessage(String server, Int32 port, String message)
    {
        try
        {
            // Create a TcpClient.
            TcpClient client = new TcpClient(server, port);

            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

            // Get a client stream for reading and writing.
            NetworkStream stream = client.GetStream(); 

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);


            // Buffer to store the response bytes receiver from the running server.
            data = new Byte[256];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

            // Close everything.
            stream.Close();
            client.Close(); return true;
        }
        catch (ArgumentNullException e)
        {
            _txtStyling.WriteCustomLine(string.Format("ArgumentNullException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
        }
        catch (SocketException e)
        {
            _txtStyling.WriteCustomLine(string.Format("SocketException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
        }
    }

【讨论】:

  • 这是连接到 SocketIO 吗?那么我想通过 Uri 传递的查询呢?
  • 啊,好吧,我知道你在找什么,看来你有一个需要在你的应用程序中使用的 Web API...为此你可以使用 RestSharp.dll ,从这里的 nuget 下载它你可以看看一些基本的例子restsharp.org/getting-started/#basic-usage
  • 我尝试连接,intsall 必要的库,导入,使用但运行时我得到...................... ..................................................... ..................................................... ...... 异常类型:System.IO.FileNotFoundException 异常消息:无法加载文件或程序集'RestSharp,版本 = 106.0.0.0,文化 = 中性,PublicKeyToken = 598062e77f915f75'或其之一依赖关系。系统找不到指定的文件。
  • restsharp 的版本应该和你的 .net 框架版本匹配,请检查
  • .NET 版本是 4.6,我无法理解它的兼容版本
猜你喜欢
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 2014-02-16
  • 2019-05-30
  • 2019-10-20
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多