Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebSocketClient ReceiveAsync从未完成(Mono、Linux)_C#_Linux_Mono_System.net.websockets - Fatal编程技术网

C# WebSocketClient ReceiveAsync从未完成(Mono、Linux)

C# WebSocketClient ReceiveAsync从未完成(Mono、Linux),c#,linux,mono,system.net.websockets,C#,Linux,Mono,System.net.websockets,我在Mono、Linux和ARM上使用WebSocket时遇到了麻烦(如果有必要的话,它是Jetson Nano) 本例连接到WebSocket服务器,然后等待返回的第一条文本消息,记录它,就这样。此程序在Windows下正常工作。但是,当我在Nano(armlinux、Ubuntu、Mono、frameworkv4.6.1)上运行它时,它从未收到消息。它连接并发出ReceiveAsync,但ReceiveAsync从未完成 我的测试WebSocket服务器只是在连接后立即返回一条“hello”

我在Mono、Linux和ARM上使用WebSocket时遇到了麻烦(如果有必要的话,它是Jetson Nano)

本例连接到WebSocket服务器,然后等待返回的第一条文本消息,记录它,就这样。此程序在Windows下正常工作。但是,当我在Nano(armlinux、Ubuntu、Mono、frameworkv4.6.1)上运行它时,它从未收到消息。它连接并发出ReceiveAsync,但ReceiveAsync从未完成

我的测试WebSocket服务器只是在连接后立即返回一条“hello”文本消息,服务器工作正常,因为其他客户端可以正确连接并接收hello消息。只是这个C#Mono版本不起作用

它使用的是wss(HTTPS上的WebSockets),服务器在开发过程中恰好是一个自签名证书,因此ServerCertificateValidationCallback返回true以允许它

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WebSocketClientTest
{
    class Program
    {

        static ManualResetEvent exitEvent = null;

        static void HandleMessage(System.Net.WebSockets.WebSocketMessageType type, byte[] buffer, int count)
        {
            Console.WriteLine(string.Format("Received {0} message of {1} bytes", type.ToString(), count));
            if (type == WebSocketMessageType.Text)
            {
                string text = Encoding.UTF8.GetString(buffer, 0, count);
                Console.WriteLine(text);
                exitEvent.Set();
            }
        }

        static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
            exitEvent = new ManualResetEvent(false);
            var webSocket = new ClientWebSocket();
            string uri = "wss://localhost:8333/api/ws";
            Console.WriteLine("Connecting to {0}", uri);
            try
            {
                webSocket.ConnectAsync(new Uri(uri), CancellationToken.None).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            Console.WriteLine("Connected");
            Action receive = null;
            receive = delegate
            {
                byte[] buffer = new byte[32768];
                webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        Console.WriteLine("Receive fault.");
                        return;
                    }
                    if (t.IsCanceled)
                    {
                        Console.WriteLine("Cancel fault.");
                        return;
                    }
                    HandleMessage(t.Result.MessageType, buffer, t.Result.Count);

                    receive();
                });
            };
            receive();

            exitEvent.WaitOne();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Security;
使用System.Net.WebSockets;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
命名空间WebSocketClientTest
{
班级计划
{
静态手动复位事件退出事件=null;
静态无效HandleMessage(System.Net.WebSockets.WebSocketMessageType类型,字节[]缓冲区,整数计数)
{
WriteLine(string.Format(“接收到{1}字节的{0}消息”,type.ToString(),count));
if(type==WebSocketMessageType.Text)
{
string text=Encoding.UTF8.GetString(缓冲区,0,计数);
控制台写入线(文本);
exitEvent.Set();
}
}
静态void Main(字符串[]参数)
{
ServicePointManager.ServerCertificateValidationCallback=新的RemoteCertificateValidationCallback(委托{return true;});
ExiteEvent=新手动重置事件(假);
var webSocket=newclientwebsocket();
字符串uri=”wss://localhost:8333/api/ws";
WriteLine(“连接到{0}”,uri);
尝试
{
ConnectAsync(新Uri(Uri),CancellationToken.None);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
返回;
}
控制台。写入线(“连接”);
动作接收=null;
接收=委派
{
字节[]缓冲区=新字节[32768];
webSocket.ReceiveAsync(新数组分段(缓冲区),CancellationToken.None)。ContinueWith(t=>
{
如果(t.IsFaulted)
{
控制台写入线(“接收故障”);
返回;
}
如果(t.IsCanceled)
{
控制台写入线(“取消故障”);
返回;
}
HandleMessage(t.Result.MessageType、buffer、t.Result.Count);
接收();
});
};
接收();
exitEvent.WaitOne();
}
}
}
我通过运行xbuild构建了这个程序


杰森纳米;Linux 4.9.140-tegra aarch64;mono 4.6.2

@SushiHangover是的,对于此测试用例,服务器也位于本地主机的Nano上。尽量让事情简单化。当然,我可以替换所需服务器的地址。@over,是的,本地websocket服务器工作正常。web浏览器可以很好地连接到它并接收hello消息,例如。mono 4.6.2有点旧,试试mono 5.x只是一种预感,您可以尝试使用
ConnectAsync()。ConfigureAwait(false)
?@SushiHangover是的,对于此测试用例,服务器也在本地主机上的Nano上。尽量让事情简单化。当然,我可以替换所需服务器的地址。@over,是的,本地websocket服务器工作正常。web浏览器可以很好地连接到它并接收hello消息,例如,mono 4.6.2有点旧,试试mono 5.x只是一种预感,您可以尝试使用
ConnectAsync().ConfigureAwait(false)