title: WebSockets - ASP.NET 4.5 IIS 8 Final Release
body: <p>All the blogs online have been in reference to the beta with the old namespaces and use nuget packages. Im trying to get a simple low level websockets example going based on whats in the wild now and its just not working. </p>
  
  <p>The client is never able to establish a connection. The error from the chrome debug console is:
  "Port error: Could not establish connection. Receiving end does not exist. "</p>
  
  <p>However, I know the request is being received because I put code in my ashx handler to email me at various points just to confirm the request was coming in and my task was firing etc.</p>
  
  <p>Config - All Final Release Versions:
  Windows Server 2012/IIS 8/ ASP.NET 4.5</p>
  
  <p>My sample is based on:
  <a href="http://www.asp.net/vnext/overview/whitepapers/whats-new#_Toc318097383" rel="nofollow">http://www.asp.net/vnext/overview/whitepapers/whats-new#_Toc318097383</a></p>
  
  <p>The handler:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.WebSockets;
  using System.Net.WebSockets;
  using System.Threading;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace myWebSocket
  {
  /// &lt;summary&gt;
  /// Summary description for wsHandler
  /// &lt;/summary&gt;
  public class wsHandler : IHttpHandler
  {
  
      public void ProcessRequest(HttpContext context)
      {
          // context.Response.ContentType = "text/plain";
          // context.Response.Write("Hello World");
  
  
          context.AcceptWebSocketRequest(MyWebSocketTask);
      }
  
      public async Task MyWebSocketTask(WebSocketContext context)
      {
          WebSocket socket = context.WebSocket;
  
  
          while (true)
          {
              System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
              message.To.Add("email@address.com");
              message.Subject = "Handler";
              message.From = new System.Net.Mail.MailAddress("michaelo@hostcollective.com");
              message.Body = string.Format("Task Launched {0}", socket.State.ToString());
              System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
              smtp.Send(message);
  
  
              ArraySegment&lt;byte&gt; buffer = new ArraySegment&lt;byte&gt;(new byte[1024]);
  
              // Asynchronously wait for a message to arrive from a client
              WebSocketReceiveResult result =
                      await socket.ReceiveAsync(buffer, CancellationToken.None);
  
              // If the socket is still open, echo the message back to the client
              if (socket.State == WebSocketState.Open)
              {
                  string userMessage = Encoding.UTF8.GetString(buffer.Array, 0,
                          result.Count);
                  userMessage = "You sent: " + userMessage + " at " +
                          DateTime.Now.ToLongTimeString();
                  buffer = new ArraySegment&lt;byte&gt;(Encoding.UTF8.GetBytes(userMessage));
  
                  // Asynchronously send a message to the client
                  await socket.SendAsync(buffer, WebSocketMessageType.Text,
                          true, CancellationToken.None);
              }
              else { break; }
          }
      }
      public bool IsReusable
      {
          get
          {
              return true;
          }
      }
  }
  }
  </code></pre>
  
  <p>As for the client, this is the simplest one based on the stuff at websockets.org</p>
  
  <pre><code>&lt;script language="javascript" type="text/javascript"&gt;
  
  // var wsUri = "ws://echo.websocket.org/";
  var wsUri = "ws://iis8hosting.com/mikey/wshandler.ashx";
  var output;
  
  function init() {
      output = document.getElementById("output");
      testWebSocket();
  }
  
  function testWebSocket() {
      websocket = new WebSocket(wsUri);       
  
      websocket.onopen = function (evt) { onOpen(evt) };
      websocket.onclose = function (evt) { onClose(evt) };
      websocket.onmessage = function (evt) { onMessage(evt) };
      websocket.onerror = function (evt) { onError(evt) };
  }
  
  function onOpen(evt) {
      writeToScreen("CONNECTED");
      doSend("WebSocket rocks");
  }
  
  function onClose(evt) {
      writeToScreen("DISCONNECTED");
  }
  
  function onMessage(evt) {
      writeToScreen('&lt;span style="color: blue;"&gt;RESPONSE: ' + evt.data + '&lt;/span&gt;');
      websocket.close();
  }
  
  function onError(evt) {
      writeToScreen('&lt;span style="color: red;"&gt;ERROR:&lt;/span&gt; ' + evt.data);
  }
  
  function doSend(message) {
      writeToScreen("SENT: " + message);
      websocket.send(message);
  }
  
  function writeToScreen(message) {
      var pre = document.createElement("p");
      pre.style.wordWrap = "break-word";
      pre.innerHTML = message;
      output.appendChild(pre);
  }
  
  window.addEventListener("load", init, false);
  </code></pre>
  
  <p>    </p>
  
  <pre><code>&lt;h2&gt;WebSocket Test&lt;/h2&gt;&lt;div id="output"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Anybody have any ideas or another simple sample such as this that is confirmed to work with the final release stuff. Thanks for taking the time to read and respond. </p>