簡體   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