WebSocket - After sending the second message I got: A server must not mask any frames that it sends to the client

Viewed 209

I'm receiving first message Successfully, but the second ( same message ) i got the Error :

A server must not mask any frames that it sends to the client.

Its only one client.

  private void RunWebSocketListener()
    {
        int websocket_port = 8996;
        TcpListener websocket = new TcpListener(Address, websocket_port);
        websocket.Start();
        bool websocket_isRunning = true;
        while (websocket_isRunning)
        {
            Console.WriteLine("--- Waiting for WebSocket ----");
            TcpClient websocket_client = websocket.AcceptTcpClient();
            NetworkStream sockStream = websocket_client.GetStream();
            byte[] bRead = new byte[1024];
            sockStream.Read(bRead, 0, bRead.Length);
            Console.WriteLine("---- Reading WebSocket ----");
            string websocket_header = Encoding.UTF8.GetString(bRead);

            string matchs = new System.Text.RegularExpressions.Regex("Sec-WebSocket-Key: (.*)").Match(websocket_header).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
            string finakAcceptedKey = Convert.ToBase64String(System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(matchs)));

            // write response
            string responseHeader = "";
            responseHeader += "HTTP/1.1 101 Switching Protocols\r\n";
            responseHeader += "Connection: Upgrade\r\n";
            responseHeader += "Upgrade: websocket\r\n";
            responseHeader += "Sec-WebSocket-Accept: "+ finakAcceptedKey +"\r\n";
            responseHeader += "\r\n";
            byte[] sendResponse = Encoding.UTF8.GetBytes(responseHeader);
            sockStream.Write(sendResponse, 0, sendResponse.Length);

            bool clientConnected = true;
            while (clientConnected)
            {
                byte[] msg = this.encodeMessage("{ \"msg\": \"Your message\" }");
                // The Problem Writing on a Second time
                sockStream.Write(msg);
                Console.WriteLine("--- msg Sent "+ msg.Length +" Bytes ---");
                Thread.Sleep(2000);
            }
            Thread.Sleep(2000);
        }
    }

#IOException on C#:

Unable to write data to the transport connection: An established connection was aborted by the software in your host machine..

JavaScript:

var websocket = new WebSocket("ws://LOCAL:PORT");
websocket.onopen = ....
websocket.onmessage = function(e) { console.log(e.data); }

#JavaScript Error:

failed: A server must not mask any frames that it sends to the client.
  • Do You Receive the first Message ? [ Yes i do ].
  • AcceptTcpClient To AcceptSocket [ not working ].
  • I'm using Sleep 2 seconds [ not working ].
  • new Threads [ not working ].
1 Answers

Fixed, The this.encodeMessage method i was return a larger bytes,

byte[] b = new byte[message.length + 5]

To:

byte[] b = new byte[message.length + 2]
Related