2

This has been discussed here but without a conclusion.

I would like to set up a WebSocket server in a self hosted Windows Service where I currently have multiple REST web services running on WCF. My requirements are not that advanced, I just need to consume an incoming stream of data. The problem is that I cannot change the way the data is send, if I could I would just convert it to a streamed HTTP POST, but this is not possible as the protocol is fixed.

According to Microsoft the WebSocket Class should be available from .NET Framework 4.5 and I am using .NET Framework 4.8 that is also supported but nowhere can I find how to self host it. There are many examples using IIS which I do not use.

So what I want to know is, 1) can the WebSocket class be used to self host a WebSocket Server inside a Windows Service? If so how is it done? Looking for some example code.

EDIT I think this might be somewhat undocumented but I finally got it to work. The magic seem to be replacing localhost or * with +. The following seem to work hosting the WebSocket while maintaining working WCF web services.

WebSocketsServer.Start("http://+:80/Socket/");

Some of it was described here but I could not make out when to use + or * and what their actual uses are.

When a port is specified, the host element can be replaced with "*" to indicate that the HttpListener accepts requests sent to the port if the requested URI does not match any other prefix. For example, to receive all requests sent to port 8080 when the requested URI is not handled by any HttpListener, the prefix is http://*:8080/. Similarly, to specify that the HttpListener accepts all requests sent to a port, replace the host element with the "+" character. For example, https://+:8080. The "*" and "+" characters can be present in prefixes that include paths.

I still do not think this makes sense fully. Anyone have a reference to why this would work?

www.jensolsson.se
  • 3,023
  • 2
  • 35
  • 65
  • 1
    If you're not married to the Framework, you could use .NET5 which includes [Kestrel](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel). It being a full-fledged web server makes it more straightforward to do WebSocketing. I think Kestrel can be run on Framework 4.x, though I've never seen it done in the wild. – NPras Sep 13 '21 at 02:04
  • 1
    You can take a look at this example:[WCF self-hosted WebSocket Service with Javascript client](https://stackoverflow.com/questions/24239953/wcf-self-hosted-websocket-service-with-javascript-client) – Jiayao Sep 13 '21 at 08:34
  • @Jiayao I tried this code before but I could not get it to work. It compiles and runs but there is no documentation or comments on how to get it to work. I tried running start with "ws://localhost/Socket" and also with "http://localhost/Socket" and it did not work. Tried to make a simple test using curl but I get the response HTTP/1.1 400 This service does not support WebSocket connections. – www.jensolsson.se Sep 13 '21 at 21:27
  • @NPras will eventually upgrade to .NET5 I think but I have around 50 other REST web services and I think it would be a too big update right now – www.jensolsson.se Sep 13 '21 at 21:28
  • @Jiayao tried that code again and it worked but there is a major showstopper that it cannot coexist with my REST web services via WCF. If I would switch to port 81 the code seem to work. But I would like to run on standard port 80. Is HttpListener not compatible with WCF ? – www.jensolsson.se Sep 13 '21 at 22:12
  • @www.jensolsson.se You can check to see if the server port trusts the application or is not working because port 80 is occupied by the application. – Jiayao Sep 14 '21 at 07:21
  • @www.jensolsson.se is it possible to post some code , i am doing the same thing but cant find any examples online – user3067170 Apr 27 '23 at 20:39

0 Answers0