title: .NET web socket returns a 200 error
body: <p><a href="https://alexjmackey.wordpress.com/2012/05/01/websockets-with-asp-net-4-5-and-visual-studio-11/" rel="nofollow">This simple web socket example</a> is returning a 200 error.</p>
  
  <p>Edit: I am reposting the code in C# in hopes that more people will be able to advise me on why I'm having this issue. </p>
  
  <p>I am running VS2012 Express, on my local IIS Machine, the project is configured for 4.5.1 framework and I have imported the Nuget Microsoft.Websockets package.</p>
  
  <p>The three pieces of code I have included below are the only three pieces of code in the project and I have made no modifications to the rest of the project.</p>
  
  <p>There is no break before the unexpected error, it does not break on open or on message on either side. The 200 comes up as an error in the chrome console, but there is no response preview.</p>
  
  <p>Here is the client (index.htm):</p>
  
  <pre><code>    &lt;!doctype html&gt;
      &lt;html&gt;
      &lt;head&gt;
  
      &lt;title&gt;&lt;/title&gt;
      &lt;script src="Scripts/jquery-1.8.1.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="test.js" type="text/javascript"&gt;&lt;/script&gt;
  
      &lt;/head&gt;
  
  
      &lt;body&gt;
      &lt;input id="txtMessage" /&gt;
      &lt;input id="cmdSend" type="button" value="Send" /&gt;
      &lt;input id="cmdLeave" type="button" value="Leave" /&gt;
      &lt;br /&gt;
      &lt;div id="chatMessages" /&gt;
      &lt;/body&gt;
  
  
      &lt;/html&gt;
  </code></pre>
  
  <p>and the client script (test.js): </p>
  
  <pre><code>$(document).ready(function () {
  
      var name = prompt('what is your name?:');
  
      var url = 'ws://' + window.location.hostname + window.location.pathname.replace('index.htm', 'ws.ashx') + '?name=' + name;
  
      alert('Connecting to: ' + url);
  
      var ws = new WebSocket(url);
  
      ws.onopen = function () {
          $('#messages').prepend('Connected &lt;br/&gt;');
          $('#cmdSend').click(function () {
              ws.send($('#txtMessage').val());
              $('#txtMessage').val('');
          });
      };
  
      ws.onmessage = function (e) {
          $('#chatMessages').prepend(e.data + '&lt;br/&gt;');
      };
  
      $('#cmdLeave').click(function () {
          ws.close();
      });
  
      ws.onclose = function () {
          $('#chatMessages').prepend('Closed &lt;br/&gt;');
      };
  
      ws.onerror = function (e) {
          $('#chatMessages').prepend('Oops something went wrong&lt;br/&gt;');
      };
  
  });
  </code></pre>
  
  <p>Here is the generic handler (ws.ashx): </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using Microsoft.Web.WebSockets;
  namespace WebSockets
  {
      public class ws : IHttpHandler
      {
          public void ProcessRequest(HttpContext context)
          {
              if (context.IsWebSocketRequest)
                  context.AcceptWebSocketRequest(new TestWebSocketHandler());
          }
          public bool IsReusable
          {
              get
              {
                  return false;
              }
          }
      }
  }
  </code></pre>
  
  <p>Here is the class (TestWebSocketHandler):</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading;
  using System.Web;
  using Microsoft.Web.WebSockets;
  namespace WebSockets
  {
      public class TestWebSocketHandler : WebSocketHandler
      {
          private static WebSocketCollection clients = new WebSocketCollection();
          private string name;
  
          public override void OnOpen()
          {
              this.name = this.WebSocketContext.QueryString["name"];
              clients.Add(this);
              clients.Broadcast(name + " has connected.");
          }
  
          public override void OnMessage(string message)
          {
              clients.Broadcast(string.Format("{0} said: {1}", name, message));
          }
  
          public override void OnClose()
          {
              clients.Remove(this);
              clients.Broadcast(string.Format("{0} has gone away.", name));
          }
  
      }
  }
  </code></pre>