title: cant decode a message from a Websocket
body: <p>I`m trying to connect my HTML/JS client to my C# server as a part of a university project in order to allow the user real-time notification. (I just need the server to be able to send a specific user a message at any given time)</p>
  
  <p>My server Is just a mock in order to implement it in my project.</p>
  
  <p>I Successfully passed the handshake stage and I am trying to send a plain string from the server to the client. I read something about Encoding the message is a way that the client will not give the "One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1" error but without success.</p>
  
  <p>How can I send primitive data through the Sockets and decode them on the client?</p>
  
  <p>My server code:</p>
  
  <pre><code>while (true)
  {
      TcpListener sck = new TcpListener(IPAddress.Any, 7878);
      sck.Start(1000);
      TcpClient client = sck.AcceptTcpClient();
  
      NetworkStream _stream = client.GetStream();
      StreamReader clientStreamReader = new StreamReader(_stream);
      StreamWriter clientStreamWriter = new StreamWriter(_stream);
      while (true)
      {
          while (!_stream.DataAvailable) ;
          Byte[] bytes = new Byte[client.Available];
          _stream.Read(bytes, 0, bytes.Count());
          String data = Encoding.UTF8.GetString(bytes);
  
          if (Regex.IsMatch(data, "^GET"))
          {
              const string eol = "\r\n"; // HTTP/1.1 defines the sequence CR LF as the end-of-line marker
  
              Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + eol
                  + "Connection: Upgrade" + eol
                  + "Upgrade: websocket" + eol
                  + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
                      System.Security.Cryptography.SHA1.Create().ComputeHash(
                          Encoding.UTF8.GetBytes(
                              new System.Text.RegularExpressions.Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                              )
                          )
                      ) + eol
                  + eol);
  
              _stream.Write(response, 0, response.Length);
          }
          else
          {
  
          }
      }
  }
  </code></pre>
  
  <p>My Client Code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function WebSocketTest() {
      if ("WebSocket" in window) {
          alert("WebSocket is supported by your Browser!");
  
          // Let us open a web socket
          var ws = new WebSocket("ws://localhost:7878");
  
          ws.onopen = function () {
  
              // Web Socket is connected, send data using send()
              ws.send("Message to send");
              alert("Message is sent...");
          };
  
          ws.onmessage = function (evt) {
              var received_msg = evt.data;
              alert("Message is received...");
          };
  
          ws.onclose = function () {
  
              // websocket is closed.
              alert("Connection is closed...");
          };
      } else {
  
           // The browser doesn't support WebSocket
           alert("WebSocket NOT supported by your Browser!");
       }
   }
   &lt;/script&gt;
  </code></pre>