title: C# as Websocket server for HTML5 websocket connection
body: <p>I guess it's already time that I ask others. Is it possible to create a websocket server using C# and server request from HTML5 codes?</p>
  
  <p>I am currently using the System package for websocket. I have a code that I downloaded over the internet and here it is:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace WebSocketChatServer
  {
      using WebSocketServer;
  
      class ChatServer
      {
          WebSocketServer wss;
          List&lt;User&gt; Users = new List&lt;User&gt;();
          string unknownName = "john doe";
  
  
          public ChatServer()
          {
  
              // wss = new WebSocketServer(8181, "http://localhost:8080", "ws://localhost:8181/chat");
  
              wss = new WebSocketServer(8080, "http://localhost:8080", "ws://localhost:8080/dotnet/Chats");
  
  
              wss.Logger = Console.Out;
              wss.LogLevel = ServerLogLevel.Subtle;
              wss.ClientConnected += new ClientConnectedEventHandler(OnClientConnected);
              wss.Start();
              KeepAlive();
  
  
  
  
          }
  
          private void KeepAlive()
          {
              string r = Console.ReadLine();
              while (r != "quit")
              {
                  if(r == "users")
                  {
                      Console.WriteLine(Users.Count);
                  }
                  r = Console.ReadLine();
              }
          }
  
  
  
          void OnClientConnected(WebSocketConnection sender, EventArgs e)
          {
              Console.WriteLine("test");
              Users.Add(new User() { Connection = sender });
              sender.Disconnected += new WebSocketDisconnectedEventHandler(OnClientDisconnected);
              sender.DataReceived += new DataReceivedEventHandler(OnClientMessage);
  
          }
  
          void OnClientMessage(WebSocketConnection sender, DataReceivedEventArgs e)
          {
              Console.WriteLine(sender);
              User user = Users.Single(a =&gt; a.Connection == sender);
              if (e.Data.Contains("/nick"))
              {
                  string[] tmpArray = e.Data.Split(new char[] { ' ' });
                  if (tmpArray.Length &gt; 1)
                  {
                      string myNewName = tmpArray[1];
                      while (Users.Where(a =&gt; a.Name == myNewName).Count() != 0)
                      {
                          myNewName += "_";
                      }
                      if (user.Name != null)
                          wss.SendToAll("server: '" + user.Name + "' changed name to '" + myNewName + "'");
                      else
                          sender.Send("you are now know as '" + myNewName + "'");
                      user.Name = myNewName;
                  }
              }
              else
              {
                  string name = (user.Name == null) ? unknownName : user.Name;
                  wss.SendToAllExceptOne(name + ": " + e.Data, sender);
                  sender.Send("me: " + e.Data);
              }
          }
  
          void OnClientDisconnected(WebSocketConnection sender, EventArgs e)
          {
              try
              {
  
                  User user = Users.Single(a =&gt; a.Connection == sender);
                  string name = (user.Name == null) ? unknownName : user.Name;
                  wss.SendToAll("server: "+name + " disconnected");
                  Users.Remove(user);
              }
              catch (Exception exc)
              {
  
                  Console.WriteLine("ehm...");
              }
  
          }
      }
  }
  </code></pre>
  
  <p>And I have this code for client side:</p>
  
  <pre><code>&lt;!HTML&gt;
      &lt;head&gt;
  
          &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;
          &lt;/script&gt;
  
          &lt;script language="javascript" type="text/javascript"&gt;  
  
          jQuery(document).ready(function(){
              var socket = new WebSocket("ws://localhost:8080");  
              socket.onopen = function(){  
                  alert("Socket has been opened!");  
              }  
          });
          &lt;/script&gt;  
      &lt;/head&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>As I run my C# console app and load the client page, the app tells me that there's someone who connected in the port it is listening to. But on my client side, as I look in firebug's console, it gives me the beginner's classic error:</p>
  
  <pre><code>Firefox can't establish a connection to the server at ws://localhost:8080/
  </code></pre>
  
  <p>What I would like to achieve is establish first a successful websocket connection and push value to my client coming from my server.</p>
  
  <p>I have considered Alchemy but the version I have is Visual Studio express 2010, the free version, and it says that "solution folders are not supported in this version of application".  </p>
  
  <p>Any help will be very much appreciated.</p>