Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net 4.5 WebSocket Server Running on Windows 7?

I know the ClientWebSocket class of .Net 4.5 is not supported on Windows 7, but is it possible to create a WebSocket server running on Windows 7 using the .Net 4.5 API?

To make myself clearer, according to both here and here, it looks like the server side part of the .Net 4.5 WebSocket implementation should be supported even on Windows 7, yet running a HttpListener and trying to access it using an open-source WebSocket implementation got me a "Portocol not supported" error

like image 972
sternr Avatar asked Aug 22 '12 12:08

sternr


People also ask

Does .NET support WebSockets?

ASP.NET Core SignalR is a library that simplifies adding real-time web functionality to apps. It uses WebSockets whenever possible. For most applications, we recommend SignalR over raw WebSockets.

How do I enable WebSockets on my server?

Windows 8 or Windows 8.1 - In Control Panel, click Programs and Features, and then click Turn Windows features on or off. Expand Internet Information Services, expand World Wide Web Services, expand Application Development Features, and then select WebSocket Protocol. Click OK. Click Close.

How do I run a WebSocket server on localhost?

What you can do is place all your code inside onopen event handler that you want to execute on successful connection. So it would be like... var webSocket = new WebSocket("ws://localhost:8025/myContextRoot"); webSocket. onopen = function() { // code you want to execute };


2 Answers

The OS-level HTTP.SYS support for websockets is limited to Win8 / Windows Server 2012 - which I agree is silly (it should be part of a windows-update, or a service-pack at most, IMO).

This means that you can't use the framework support for WebSockets from HttpListener or ASP.NET directly.

But: as for "is it possible to create a WebSocket server" - sure... but only if you handle the TCP/IP comms yourself, or use a 3rd-party library for the same. This is a little annoying, but is not as bad as it might sound.

Edit: after some checking, I can confirm that the server-side components for this do not work on Windows 7 (etc); the IsWebSocketRequest returns false even though it is a web-socket request with Connection: Upgrade and Upgrade: websocket (etc) headers (from a Chrome session, as it helps).

I am, however, very surprised to find that the client-side pieces don't work, because: that is simpler and doesn't (or at least, doesn't need to) involve HTTP.SYS. Trying to use them throws a PlatformNotSupportedException.

like image 85
Marc Gravell Avatar answered Oct 06 '22 06:10

Marc Gravell


As Marc says, the Microsoft APIs do not work on Windows 7. However there are several open source libraries that support WebSockets on Windows 7, and in some cases even cross platform via Mono.

  • https://github.com/statianzo/Fleck
  • http://vtortola.github.io/WebSocketListener/
  • http://superwebsocket.codeplex.com/
like image 38
Drew Noakes Avatar answered Oct 06 '22 06:10

Drew Noakes