Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare WebSocket Development kits of .NET

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people do not know what to do about how to compare the WebSocket development packages of .NET. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Web projects often need to push data to the customer as quickly as possible without waiting for a client request if necessary. For websites that communicate with users in real time, such as online communication or document collaboration tools, or updating system status on long-running computing / task servers, and so on, it is ideal to adopt a two-way communication mechanism.

In the past, the following solutions were commonly used for such problems:

Use the Socket connection in Flash (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html)

Ajax long polling (https://gist.github.com/jasdeepkhalsa/4353139)

The server sends events. (http://en.wikipedia.org/wiki/Server-sent_events)

... Or just use the classic Frame technology in IE (http://cometdaily.com/2007/11/05/the-forever-frame-technique/))

But now we have a better choice: WebSocket. Its standard was released in 2011 and has been implemented on modern browsers for some time. It is better because it uses more secure and mature protocols, resulting in improvements and upgrades.

A brief note:

This comparison was made a few months ago and may not be timely, but if someone is looking for a good WebSocket library, I think it is still useful for him.

This comparison is only for libraries released as NuGet packages. Although SuperWebSocket uses NuGet's repository, it needs to be downloaded from the web page.

Maybe when I find the time, I will use the new library or the new version of the tested library to compare and update this article.

Fleck

Https://github.com/statianzo/Fleck

I find this library really easy to use, for libraries, documents, examples, etc., just add the library, copy the code from a few lines of example, and then run it-it's as simple as that.

But simplicity comes at a price: it is not powerful, and there are too few places to configure.

Private static void Main (string [] args) {var server = new WebSocketServer ("ws://localhost:8181"); server.Start (socket = > {socket.OnOpen = () = > OnOpen (socket); socket.OnClose = () = > OnClose (socket); socket.OnMessage = m = > OnMessage (socket, m);});}

I'll use it for simple and fast projects, and if you don't need to use WebSocket to send overly complex data structures, command-like messages, or alternatives when the client doesn't have WebSocket support, that's what you want.

Advantages:

simple

No dependency

Disadvantages:

Few configurable items

Not when client browsers don't support WebSocket.

SignalR

Http://www.asp.net/signalr

Microsoft's production is what I think is the advantage of this library. It has been integrated with the existing ASP.NET framework and has a good abstract class for both server-side and client-side code, which means you don't need to know much about the protocol. It can then cleverly use other communication mechanisms when client browsers do not support WebSocket. It can also do something called remote procedure call (RPC), from the server to the client.

It can broadcast messages to all clients or send them to specified users separately. The handling of a large number of concurrent connections is also excellent. And-- it's open source!

Sounds great, doesn't it? But. It needs IIS8 or Windows Server 2012 (Windows8 is fine, but I'm sure you won't run a big project on win8). To me, this is the cool feature of "Microsoft's next-generation operating system worth buying". It would be nice to develop an enterprise project, but for small projects, buying an operating system for this open source library is too expensive.

Of course, these environments are required by WebSocket. This article is about WebSocket communication, so I count this as a major disadvantage.

Public class MyHub1: Hub {public void Send (string name, string message) {/ / Call the broadcastMessage method to update clients. Clients.All.broadcastMessage (name, message);} $(function () {var chat = $.connection.myHub1; chat.client.broadcastMessage = function (name, message) {/ /...}; $.connection.hub.start () .done (function () {$('# sendmessage') .click (function () {chat.server.send ('message')) });)

Advantages:

Very good abstraction.

Tight integration with IIS and ASP.NET

There are many candidate ways

Open source

Microsoft official library

Good scalability

Disadvantages:

Need IIS8...

... That is, Windows Server 2012 is too expensive.

AlchemyWebSocket

Http://alchemywebsockets.net/

When I think of the websocket library, this is incredible. Yes, it's true. It can be ranked after Fleck, it is very easy to use, easy to install (the Nuget package is available), and the documentation contains good examples.

It consists of two parts: server and client, and it is also scalable.

Static void Main (string [] args) {/ / create a new server-accept port and ip range, / / set method var aServer = new WebSocketServer (81, IPAddress.Any) {OnReceive = OnReceive, OnSend = OnSend, OnConnect = OnConnect, OnConnected = OnConnected, OnDisconnect = OnDisconnect, TimeOut = new TimeSpan (0,5,0)} AServer.Start (); string consoleReadLine; do {consoleReadLine = Console.ReadLine (); sockets.ForEach (s = > s.Send (consoleReadLine));} while (consoleReadLine! = "exit");}

But it's a little awkward, and I can't avoid it. For example, there is no simple event method "OnReceive", only string, in fact, the message is sent on the client. You have to do it yourself. Yes, you must call, and you can only call .ToString () to get the real message, but the purpose of using the library is not to force yourself to implement the communication protocol.

Private static void OnReceive (UserContext context) {Console.WriteLine ("Client" + context.ClientAddress.ToString () + "sended:" + context.DataFrame.ToString ());}

The WebSocket server initialization method first receives the port and then the IP setting. I have always thought that the expression of the address should be IP first and then the port, and only if it is necessary to specify the port. There is also a timeout setting: why must there be a timeout? I can understand that this can be useful sometimes, but it should not be one of the main settings as a feature. Of course, these are just details.

For me, this forces you to abstract it from this library with another layer of code in the first place.

Anyway, you can try it, compare the performance with Fleck, and then decide which one is more suitable for your simple project.

Advantages:

simple

No dependence

Complete documentation

Disadvantages:

It's a little clumsy and more complex than Fleck.

No fallback.

XSockets

Http://xsockets.net/

This library looks promising. I have tried it and spent a lot of time using it to work more than other libraries (even to perform testing work, and so on). But unfortunately I have no luck, any error I consider is wrong in this library, bad documentation that is inconsistent with the code. Is it because the code or document is out of date? It is not easy to install and run, in fact, it is difficult for me to build and run the sample library. Xsocket shows us more about what the MVC framework looks like. I tried to run it in the ASP.NET project, MVC and WinService, but unfortunately none of them worked.

I really wanted to use this library, but * I gave up to support a better library (read others). Seriously, why using this library is difficult, even a simple project. You can predict more problems when using it in the project, and I strongly recommend avoiding this project.

Public static class XSocketsBootstrap {private static IXBaseServerContainer wss; public static void Start () {wss = XSockets.Plugin.Framework.Composable.GetExport (); wss.StartServers ();}}

Advantages:

Seems powerful Should have good JavaScript integration

Disadvantages:

Complicated and hard Complicated to configure and run inside of WebForms, MVC and WinService Differences between code and documentation Outdated documentation and examples Microsoft.WebSocket

Http://msdn.microsoft.com/en-us/hh969243.aspx

Another library from Microsoft. And it requires IIS 8 too, so I did not have means to test it. Examples are really low level, so it force you to deal with buffers and streams instead of strings. In some cases this can be good, but mostly there is no point. If you have IIS 8 on server why bother with this library if you can use SignalR, which will take care most of the stuff for you.

I think this is more of proof-of-concept then usable library.

Int count = receiveResult.Count; while (receiveResult.EndOfMessage = = false) {if (count > = maxMessageSize) {string closeMessage = string.Format ("Maximum message size: {0} bytes.", maxMessageSize); await socket.CloseAsync (WebSocketCloseStatus.MessageTooBig, closeMessage, CancellationToken.None); return;} receiveResult = await socket.ReceiveAsync (new ArraySegment (receiveBuffer, count, maxMessageSize-count), CancellationToken.None); count + = receiveResult.Count } var receivedString = Encoding.UTF8.GetString (receiveBuffer, 0, count); var echoString = "You said" + receivedString; ArraySegment outputBuffer = new ArraySegment (Encoding.UTF8.GetBytes (echoString)); await socket.SendAsync (outputBuffer, WebSocketMessageType.Text, true, CancellationToken.None)

SuperWebsocket

The least important thing is SuperWebsocket. I'm a little skeptical about this (if I remember correctly, it's just a package I found on the NuGet website, but it's not a usable package). It seems a little complicated, but in fact it is very simple. Examples supported in the literature help you step by step from the simplest WebSocket server to a command request, JSON, multi-server instance, .config file configuration, or more complex Websocket servers.

This library may not contain all the cool features that other libraries have, but it doesn't matter, because it's highly configurable and you can easily make it do what you want. It can be run in ASP.NET as a console application or as a windows service. In the literature, it is suggested to run the server in the form of system service. In my experience, it is not recommended to run it in a web application because the solution is slow (very poor performance, about 50 times slower than a console application). On the other hand, stand-alone server applications need to run the file at the end of .exe, which is not part of the library, but part of the SuperSocket project (SuperWebSocket is based on this project). This makes you need a little skill to turn on the server in the debugging session, or to enable debugging completely. When you run the server as an application, although this is not part of the solution, you need to make sure that the server uses * * versions of components from other projects.

In return, you get a well-known solution for flexible WebSocket.

It's still open source, so you can change it as needed.

On the other hand, you might see the server's lack of a JavaScript client as its weakness (but it has a C # client). This server also has third-party dependencies.

I didn't find any major problems after working with this library for a few months.

Disadvantages and advantages:

No backup communication

Dependence

Elegant features and high degree of configurability

A great example.

All of the examples have documents with recommended settings

Can be run as windows services and ASP.NET modules and console applications

Good performance

I recommend using SuperWebSocket for complex solutions / projects because it is a stable and highly configurable library. I will choose Fleck for projects that are simple and require rapid development, but if there is a way to use * * windows server as a test and production machine, I will choose SignalR instead of using these two.

After reading the above, have you mastered how to compare the WebSocket development packages of .NET? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report