49

So, I found that amazing thing called HTML5 WebSockets, new API. That is still in DRAFT version, but quite well supported. Full-duplex bi-directional communication. I know how to use it via JavaScript, there is APIs. But if I want to use a WebSocket client within my C#/.NET application, how to do that?

For example JavaScript: http://bohuco.net/blog/2010/07/html5-websockets-example/

Are there are any special client libraries for WebSockets in .NET?

leggetter
  • 15,248
  • 1
  • 55
  • 61
davidlt
  • 1,007
  • 2
  • 11
  • 17
  • 4
    I wonder why this question was downvoted. It seems like a perfectly valid question to me. It's just that the OP doesn't explain quite well whether he is looking for implementing a WebSocket server in .NET or something else. +1 from me. – Darin Dimitrov Feb 13 '11 at 17:34
  • 14
    it makes perfect logical sense. if you want to write one server implementation that both HTML5 and Silverlight other C# clients can access then – Simon_Weaver Nov 27 '11 at 04:54
  • You may want to use Fleck, available on NuGet: http://nuget.org/packages/Fleck/ – Matt Cruikshank Apr 10 '13 at 21:35

10 Answers10

13

sir SuperWebSocket include a WebSocket server implementation and a WebSocket client implementation. SuperWebSocket's project page

Kerry Jiang
  • 1,168
  • 13
  • 10
  • Note, the client implementation has not been included in latest downloadable package, please find it in source code. – Kerry Jiang Mar 10 '11 at 08:49
  • Thanks for the info, gonna look into it in few days. Which standard it supports? draft-ietf-hybi-thewebsocketprotocol-04 (aka draft-hixie-thewebsocketprotocol-76)? – davidlt Mar 11 '11 at 04:58
  • Yes, it is a v76 implementation. – Kerry Jiang Mar 16 '11 at 08:31
  • Yesterday, it released Drop 4. http://superwebsocket.codeplex.com/releases/view/62622 – Kerry Jiang Mar 16 '11 at 08:32
  • I'm fairly sure they've ditched the client implementation now. Can anyone confirm. I can't find it *anywhere* even in the sourcecode. They seem to expect me to just use the Socket class or 'telnet' – Simon_Weaver Nov 27 '11 at 05:02
  • 8
    The client in SuperWebSocket has been separated into a new project WebSocket4Net: http://websocket4net.codeplex.com/ – Kerry Jiang Jan 09 '12 at 02:35
7

I've recently done some research into this whilst building a .NET and Silverlight client library for Pusher. I found the following WebSocket client libraries and projects:

For the moment the Microsoft implementation is probably the easiest to use and it also has a Silverlight library. SuperWebSockets has a Silverlight project in the source but not in the latest drop.

leggetter
  • 15,248
  • 1
  • 55
  • 61
  • @Simon_Weaver The SuperWebSocket client can be found [here](http://superwebsocket.codeplex.com/SourceControl/changeset/view/71423) and you'll see on the left there is a 'WebSocketClient' project/link. I can't link directly to it I'm afraid. – leggetter Dec 01 '11 at 10:55
  • It is in SourceCode/mainline/Client – Kerry Jiang Dec 08 '11 at 08:13
  • 1
    WebSocket client in SuperWebSocket had became WebSocket4Nethttp://websocket4net.codeplex.com/ – Kerry Jiang Feb 15 '12 at 03:51
5

Starting from .NET 4.5, WebSocket clients are supported via System.Net.WebSockets.ClientWebSocket

You can browse or download this sample C# app from MSDN Code website: http://code.msdn.microsoft.com/WebSockets-middle-tier-5b2972ce/sourcecode.

To the down-voters of the question, the sample is mainly focused on connecting to WebSocket services, which is another significant use-case for a network-centric C# application.

vulcan raven
  • 32,612
  • 11
  • 57
  • 93
  • 2
    One must take care that this is supported on Windows 8 upwards, not on Windows 7 / Windows Server 2008. Quoting the MSDN page: "The class elements in the System.Net.WebSockets namespace that are supported on Windows 7, Windows Vista SP2, and Windows Server 2008 are abstract class elements." – jv42 Dec 16 '16 at 10:31
  • 1
    True and if this ticket gets resolved: https://github.com/dotnet/corefx/issues/9503, then it would be supported in Windows 7 as well. Fingers crossed (seems like only a matter of packaging). – vulcan raven Jan 31 '17 at 21:27
2

You could use http://www.nuget.org/packages/Microsoft.AspNet.SignalR/ or http://www.asp.net/signalr

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and side. Servers can now push content to connected cliently instantly as it becomes available. SignalR supports Web Sockets, and falls back to other compatible techniques for older browsers. SignalR includes APIs for connection management (for instance, connect and disconnect events), grouping connections, and authorization.

surya
  • 1,351
  • 1
  • 13
  • 29
2

I haven´t tried the Microsoft implementation, but I think Xsockets has the fastest setup time (nuget package). Under 3 min from start to running a complete socketserver + client (demo chat application). Youtube demo

It has fallback to Silverlight and flash for older browsers.

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
Ben
  • 21
  • 1
1

A friend an I just released a very lightweight, lean, scalable C# websocket server: https://github.com/Olivine-Labs/Alchemy-Websockets

We built it to use in our online game, so our top concern was the quick and efficient handling of tons of connections. It's, from my research, the most efficient out there. And, as a bonus, it supports flash websockets as a fallback for users without websocket-enabled browsers.

Jack Lawson
  • 2,479
  • 3
  • 23
  • 24
1

If you're looking for a high performance enterprise WebSocket server, take a look at Kaazing. Kaazing has complete support for .NET including Xamarin.

Here is a step-by-step tutorial for AMQP: Checklist: Build Microsoft .NET AMQP Clients.

[Disclosure: I work for Kaazing.]

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Peter Moskovits
  • 4,236
  • 1
  • 20
  • 15
0

I'm deploying XSockets.net. The framework works quite good, it is being maintained, the developers offer paid support but for normal issues they are also quite active here in SO and they help a lot.

They offer a .net API for implementing the sockets and also a javascript API for the client.

As a summary, I can recommend it.

Rafa
  • 2,328
  • 3
  • 27
  • 44
0

Yes, you will need an intermediary server which supports the WebSocket protocol. This server could be written in any language including .NET. Here's one for .NET but it really could be any language. As far as your site is concerned it could be ASP.NET and the client part in javascript which will talk to the WebSocket server.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have implementation in C level, while I can use it from JavaScript I also want to use it from .NET desktop application. I want to move from pull model to push model. Push new data from server to client. I am looking for Client API on .NET. – davidlt Feb 15 '11 at 04:46
-1

You have a few choices

Scott Weinstein
  • 18,890
  • 14
  • 78
  • 115
  • As I mentioned for Darin, I am looking for Client API on .NET. I have server implemented, but I want to use this API not only on web development, but also on desktop client version. So far it looks very good use for web apps, also desktop and mobile clients. – davidlt Feb 15 '11 at 04:49