5

I'm new in web sockets and trying to create one using asp.net Generic Handler and JavaScript WebSocket Class

JavaScript

<script type="text/javascript"> window.onload= function () { var name = prompt('what is your name?:'); var url = 'ws://localhost:5707/ws.ashx?name=' + name; var ws = new WebSocket(url); ws.onopen = function () { alert('Connection Opened'); }; ws.onmessage = function (e) { }; ws.onclose = function () { alert('Connection Close'); }; ws.onerror = function (e) { alert('Error') }; } </script> 

C# Generic Handler Called ws.ashx

public class ws : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.IsWebSocketRequest) context.AcceptWebSocketRequest(new TestWebSocketHandler()); } public bool IsReusable { get { return false; } } } 

Class TestWebSocketHandler which is inherits from WebSocketHandler

 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)); } } 

My Problem is

when the websocket intend to open i noticed that when went to the handler the

context.IsWebSocketRequest // returns false 

and then it fires Error on the client says

Firefox can't establish a connection to the server at ws://localhost:5707/ws.ashx?name=j

and then close the connection instantiation

i need to know where is the problem ? kindly

i'm using vs 2013 under windows 7 and i think its IIS 6 which i work on

4
  • Have you used fiddler or similar to see what is being sent/received? Commented Mar 31, 2014 at 9:06
  • no but how it will helps Commented Mar 31, 2014 at 9:07
  • actually, you might need to use the firefox tools; change to the network tab, find the web-socket connection, and look at the headers. For example, here's mine from this site (stackoverflow.com): i.sstatic.net/goQTY.png - what do you get against your site? Also: does it work in, say, Chrome? Commented Mar 31, 2014 at 9:15
  • no it works in non of the top 5 browsers :( Commented Mar 31, 2014 at 9:34

2 Answers 2

5

WebSockets will only work on ASP.NET applications running on Windows 8 or Windows 2012 I am afraid. Despite of the API been included on .NET 4.5.1, it won't work if you are not using those operating system. Actually, if you try to use the ClientWebSocket it will throw a PlatformNotSupportedException.

If you cannot get any of those operating systems, you can check alternatives:

Sign up to request clarification or add additional context in comments.

7 Comments

may be you're right but what are these tools in the references you gave me uses to deal with websockets on .net/ on the other hand i use .net framework 3.5.1 does it not supports websockets ?? or its related to IIS Version ??
@Marwan ah, I didn't see the Win 7 - it is the OS version that matters; the web-sockets layer that this uses is baked into http.sys; however, there are alternative web-sockets APIs available, if you genuinely can't use a different OS
They all probably use plain sockets. I developed WebSocketListener, and it uses a normal TcpListener to get Socket objects and negotiate a WebSocket session. I will not work in 3.5.1 though. What new http.sys provides is the ability of transforming a normal HTTP request into a WebSocket connection, so you can run your websockets in the same port than your webpages. In the other hand, those alternatives that I put, need to be run in a different port as a service external to IIS. MS tells this was a change too big for introduce it in Windows 7 or Windows server 2008.
@vtortola of course, one option there is to simply use a NLB to send requests destined to the web-socket server to different backend nodes. Not what we do ourselves (we use a different domain for web-sockets), but it should work.
I've gone far that i can't get it i think i need a little research about networking and deep into windows ports and such a stuff so any references suggested
|
2

Reason could be any of these:

  1. Having IIS version < 8
  2. Not enabled Web Socket Protocol under windows features
  3. Having .NET Framework version < 4.5
  4. Not included the following lines in web.config's system.webServer section:
<handlers> <add path="/ws.ashx" verb="*" name="ws" type="namespace.ws"/> </handlers> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.