title: C# Websocket The remote party closed the WebSocket connection without completing the close handshake
body: <p>Hello I'm currently implementing a websocket component to my very basic site. I'm running .NET Core 3.1 HTTP Listener for serving html, I've been stumped by implementing websockets.</p>
  <p>I've worked with TCP in C# before and understand the flow of everything but websockets are a new thing to me. Here is the C# code for accepting websockets</p>
  <pre><code>        [Route(&quot;/socket&quot;, &quot;GET&quot;)]
          public static async Task upgrade(HttpListenerContext c)
          {
              if (!c.Request.IsWebSocketRequest)
              {
                  c.Response.StatusCode = 400;
                  c.Response.Close();
                  return;
              }
  
              try
              {
                  var sock = (await c.AcceptWebSocketAsync(null)).WebSocket;
                  byte[] buff = new byte[1024];
                  var r = await sock.ReceiveAsync(buff, System.Threading.CancellationToken.None);
              }
              catch (Exception x) 
              {
                  Console.WriteLine($&quot;Got exception: {x}&quot;);
              }
             
              //WebSocketHandler.AddSocket(sock);
              
          }
  </code></pre>
  <p>I've added <code>var r = await sock.ReceiveAsync(buff, System.Threading.CancellationToken.None);</code> to this function because originally I was getting the exception in my WebSocketHandler class, so I moved the code to the one function to test.</p>
  <p>Here is the client:</p>
  <pre><code>  &lt;script&gt;
      let socket = new WebSocket(&quot;ws://localhost:3000/socket&quot;);
  
      socket.onopen = function (event) {
        console.log(&quot;[ OPENED ] - Opened Websocket!&quot;);
      };
  
      socket.onclose = function (event) {
        console.log(&quot;[ CLOSED ] - Socket closed&quot;);
      };
  
      socket.onerror = function (error) {
        console.log(&quot;[ ERROR ] - Got websocket error:&quot;);
        console.error(error);
      };
      socket.onmessage = function (event) {
        // This function will be responsible for handling events
        console.log(&quot;[ MESSAGE ] - Message received: &quot;);
        const content = JSON.parse(event.data);
        console.log(content);
      };
  
    &lt;/script&gt;
  </code></pre>
  <p>Here is the output in the console for the client:</p>
  <pre><code>Navigated to http://127.0.0.1:5500/index.html
  index.html:556 [ OPENED ] - Opened Websocket!
  index.html:569 [ CLOSED ] - Socket closed
  </code></pre>
  <p>And here is the exception from the C# server:</p>
  <pre><code>Got exception: System.Net.WebSockets.WebSocketException (997): The remote party closed the WebSocket connection without completing the close handshake.
     at System.Net.WebSockets.WebSocketBase.WebSocketOperation.Process(Nullable`1 buffer, CancellationToken cancellationToken)
     at System.Net.WebSockets.WebSocketBase.ReceiveAsyncCore(ArraySegment`1 buffer, CancellationToken cancellationToken)
     at SwissbotCore.HTTP.Routes.UpgradeWebsocket.upgrade(HttpListenerContext c) in C:\Users\plynch\source\repos\SwissbotCore\SwissbotCore\HTTP\Routes\UpgradeWebsocket.cs:line 29
  </code></pre>
  <p>I can provide the http requests that the client sends if need be but I am completely stumped on this, any help will be greatly appreciated.</p>