繁体   English   中英

Mono C#中的wss WebSocket服务器:SslStream没有提供任何数据,但Stream提供了

[英]wss WebSocket server in Mono C# : SslStream gives me no data but Stream do

几天以来,我一直试图使(JS)wss /(c#)SslStream连接正常工作,以便在单声道c#中创建wss服务器。

我的问题:服务器接受传入的安全WebSocket连接时,我无法从中获取数据以开始握手过程。

我到目前为止所做的:

  1. 我设置了一个TCPListener,它接受客户端给我的TCPClient实例。
  2. 我从TCPClient获取流并从中创建一个SslStream。
  3. 我使用AuthenticateAsServer(X509Certificate2)同步对其进行身份验证
  4. 我尝试读取数据失败

其他详情 :

  1. 我注意到,如果我使用Stream对象而不是SslStream对象,则可以成功地从中获取数据(但按预期进行了加密)
  2. 我测试了使用相同的pfx证书将WebSoket连接到与Fleck相同的地址/端口(也与Monodevelop一起在我的盒子中构建),并使其正常工作
  3. 我还注意到, Fleck抛出消息说它收到了0个字节,然后它关闭了连接并(以某种方式)重新连接它并从Socket / SslStream中正确获取数据。
  4. 我没有收到任何错误,SslStream似乎已正确验证
  5. 我在https中正确连接客户端,并在同一程序中处理另一个端口上的请求

我的配置:

  • 操作系统:ArchLinux
  • C#:Mono .Net Framework 4.7
  • 浏览器(Chromium和Firefox)

    尽管到目前为止我一直在研究,但我还没有找到解决方案,也许有人可以帮助我想知道我在这里错过了什么...

在此先感谢您的帮助!

监听代码:

   public void AudioListen ()
    {
        audioComListener.Start ();
        while (isAudioActive) {
            TcpClient s = audioComListener.AcceptTcpClient ();
            Stream clientStream;
            string hellostr;

            clientStream = getClientStream(s);

            if(debugCommuncation)
            {
                logToFileLine("(KEY) HandShaking begins with "+s.Client.RemoteEndPoint.ToString ().Split (':') [0]);
            }

            if(!WebSocketHandshake(clientStream))
            {
                if(debugCommuncation)
                {
                    logToFileLine("(KEY) (X) HandShake read 0 byte from "+s.Client.RemoteEndPoint.ToString ().Split (':') [0]);
                }

                s.Close();

                return;
            }
            else
            {
                logToFileLine("(KEY) HandShaking OK with "+s.Client.RemoteEndPoint.ToString ().Split (':') [0]);
            }

            hellostr=streamReadLine(clientStream);

            if(hellostr.IndexOf("hello:")==0)
            {
                string usrstr=hellostr.Split(':')[1];
                User usr= Users.GetUser(usrstr);


                if(usr!=null)
                {
                    usr.TcpC=s;

                    User usrdest=usr.corresp;

                    if( usrdest!=null && 
                        usrdest.ByPass==null && 
                        usrdest.TcpC!=null)
                    {
                        Stream clientStream2 = getClientStream(usrdest.TcpC);
                        usr.ByPass=new TCPByPass(clientStream,clientStream2);
                        usrdest.ByPass=usr.ByPass;
                    }                       

                }
            }

            Thread.Sleep (1);
        };
    }

获取SslStream的函数:

        private Stream getClientStream(TcpClient s,bool forceHTTP=false)
    {
        Stream clientStream;
        if(isSSL && !forceHTTP)
        {
            clientStream = new SslStream (s.GetStream ());
            ((SslStream)clientStream).AuthenticateAsServer(SrvCert);
            // Set timeouts for the read and write to 5 seconds.
            /**/clientStream.ReadTimeout = 5000;
            clientStream.WriteTimeout = 5000;

            //SecuDiag.MakeAllDiag(((SslStream)clientStream));
        }
        else
        {
            clientStream=s.GetStream ();
        }     

        return clientStream;   
    }

尝试出于hanshake目的获取数据的函数:

    public bool WebSocketHandshake(Stream clientStream)
    {
        string hellostr;

        // Here I test trying to get data (Also tried to use Stream.ReadByte())
        Byte[] toto=new Byte[2048];

        ((SslStream)clientStream).Read(toto,0,2048);

        if(toto[0]==0)return false;

        Console.WriteLine("#############################################");
        Console.WriteLine("toto array is {0} bytes long",toto.Length);
        for(int t =0;t<10;t++)
        {
            for(int u =0;u<10;u++)
            {
                Console.Write(toto[t*10+u].ToString());
            }
            Console.WriteLine(";");
        }
        Console.WriteLine("#############################################");

        // Trying to get data

        //hellostr=streamReadLine(clientStream);     

        //Console.WriteLine(hellostr);

        return true;
    }

我想我解决了问题。 我不明白为什么,但是我认为有必要在接受连接之后立即关闭接受的连接。 Websocket将自动重新连接:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM