. NET WebSocket Development Package Comparison

Source: Internet
Author: User
Tags net websocket

Web projects often need to push data to customers as quickly as possible, without waiting for client requests when necessary.   A two-way communication mechanism is ideal for sites that communicate with users in real time, such as online communication or document collaboration tools, or to update system state on long-running compute/execution servers. Previously, this type of problem generally used the following solution:? Using the Socket connection (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html) in Flash? Ajax Long Polling (https://gist.github.com/jasdeepkhalsa/4353139)? Server Send Event ... (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 standards were released in 2011 and have been implemented in modern browsers for some time.   It is better to use more secure and mature protocols, bringing improvements and upgrades.   Note: This comparison was made a few months ago and may not be timely enough, but if someone is looking for a good websocket library, I think it will still be useful for him.   This comparison is only for libraries published as NuGet packages, although Superwebsocket uses NuGet repository, but needs to be downloaded from a Web page.  Maybe when I'm done, I'll use the new library or the new version of the tested library to compare and update this post.   Fleck Https://github.com/statianzo/Fleck I found that this library is really easy to use, for libraries, documents, examples and so on, just add libraries, copy the code from a few lines of examples, and then run-it's that simple. But simplicity comes at a price: it's not powerful, and there are too few places to configure. 1 2 3 4 5 6 7 8 9 private static void Main (string[] args) {var server = new Websocketserver ("ws://localhost:8181"); SE RVer. Start (Socket =&Gt {socket. OnOpen = () = OnOpen (socket); Socket. OnClose = () = OnClose (socket); Socket. OnMessage = m = = OnMessage (socket, m); });   I'll use it for a simple and fast project, which is what you need if you don't need to send too complex data structures with WebSocket, command-like messages, or alternative ways for clients without websocket support. Advantages:? Simple? No dependency disadvantage:? What are the few configurable items? The client browser does not support WebSocket. SignalR http://www.asp.net/signalr Microsoft is the biggest advantage I think the library. It has been integrated with the existing ASP. NET Framework and has done a good job of abstracting the server side and client code, which means you don't need to get too deep into the protocol. It can then be smart to automatically use other communication mechanisms when the client browser does not support WebSocket.   It can also accomplish something called a Remote Procedure call (RPC), from the server to the client. It can broadcast messages to all clients, and can be sent to designated users individually. The processing 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 (WINDOWS8), but I'm sure you won't run a big project on Win8. For me, this is the cool feature of "a new generation of Microsoft's worth of operating systems".   It's good to develop an enterprise project, but for small projects, buying an operating system for this open source Library--it's too expensive. Of course these environments are websocket must require. This article is about websocket communication, so I counted this as a big drawback. 1 2 3 4 5 6 7 8 public class Myhub1:hub {public void Send (string name, String message) {//Call the broadcastmessage m Ethod to update clients. Clients.All.broadcastMessage (name, message); }} 1 2 3 4 5 6 7 8 9 Ten $ (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? Tightly integrated with IIS and ASP. Many candidate ways? Open source? Microsoft Official Library? Scalability is a good disadvantage:? Need IIS8 ...? ... That is, Windows Server 2012 is too expensive alchemywebsocket http://alchemywebsockets.net/when I think about WebSocket library, this is amazing. Yes, that's true.   It can be lined up behind Fleck, it's very easy to use, easy to install (NuGet packages available), and the documentation contains good examples. It contains both the server and the client, and it also has the Scalability 1 2 3 4 5 6 7 8 9 Ten All in all, the static void Main (string[] args) {//Gen Build a new server-accept port and IP range,//set method var aserver = new Websocketserver (Bayi, ipaddress.any) {onreceive = onreceive, OnSend = On Send, 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 has some awkward, I can't avoid. For example thatThere is no simple event method "OnReceive", only a string, in fact the message is sent on the client. You have to do it yourself. Yes, you have to call, and you can only call. ToString () to get the real message, but the purpose of using the library is to not force itself to implement the communication protocol. 1 2 3 4 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 always thought that the expression of the address should be the first IP then the port, and only when it is necessary to specify the port. There is also a timeout setting: Why does it have to be timed out? I can understand that this can sometimes be useful, but it should not be one of the main settings as a feature.   Of course, this is only a matter of detail.   For me, it's forcing you to get it out of the library with another layer of code from the beginning.   You can try it out, compare performance with Fleck and decide which is better for your simple project. Advantage:? Simple? No dependency? Documentation Complete Disadvantages:? A bit clumsy, more complicated than the fleck structure? There is no fallback xsockets http://xsockets.net/this library looks promising. I tried it and spent a lot of time working with it over other libraries (even for testing, etc.). But unfortunately I have no luck, and any mistakes I have taken into account in this library are all wrong, bad documents that are inconsistent with the code. Is it because the code or the document expires? It is not easy to install and run, in fact, the use of this library sample I difficult to build and run. Xsocket more shows us what the MVC framework looks like.   I tried to run it inside the ASP, MVC and WinService, and unfortunately none of them could work. I really wanted to use this library, but in the end I gave up to support a better library (read more). Seriously why using this library is difficult, even for a simple project. You can predict more problems when it is used in the project, I strongly recommend to avoid this project. 1 2 3 4 5 6 7 8 9 public static class Xsocketsbootstrap {private static Ixbaseservercontainer Wss public static void Start () {WSS = XSockets.Plugin.Framework.Composable.GetExport (); WSS. Startservers (); }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

Advantages:

    • Seems powerful
    • Should has 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 didn't have the means to test it. Examples is really low level, so it force you to deal with buffers and streams instead of strings. In some cases the can good, but mostly there are no point. If you had IIS 8 on server Why bother with this library if you can use SignalR, which would take care of the very most of the stuff fo R you.

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

    int count = Receiveresult.count; while (Receiveresult.endofmessage = = False) {if (Count >= maxmessagesize) {string closemessage = String.f        Ormat ("Maximum message size: {0} bytes.", maxmessagesize); await the 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 http://superwebsocket.codeplex.com/Last but not least, superwebsocket. I have a little doubt about this (if I remember correctly, this is just a package I found through the NuGet website, but not a usable package). It seems a little complicated, but in fact it is very simple. Examples of documentation support help you step-by-point from the simplest websocket server to a command request, JSON, multi-server realexample,. config file configuration or more complex WebSocket servers. This library may not contain the cool features of all the other libraries, but it doesn't matter, because it's highly configurable and you can easily make it do what you want. It can be run as a console application or as a Windows service in ASP. In the literature, it is recommended to run the server as a system service. From my experience, it is recommended that you do not run it in a Web application because the solution is slow (very bad performance, about 50 times times slower than the console application). On the other hand, standalone server applications need to run files ending with. exe, which is not part of the library, but is part of the SuperSocket project (Superwebsocket is based on this project). This allows you to have a little bit of finesse to turn on the server in a debugging session, or to fully enable debugging.  While you are running the server as an application, although this is not part of the solution, you also need to make sure that the server uses the latest version of the 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 consider this server lacks JavaScript client as its disadvantage (but it has a C # client).  This server also has third-party dependencies.  After working with this library for a few months I didn't find any major problems. Disadvantages and Advantages: No standby communication? Is it an elegant feature and a high degree of configuration? A great example? Are there documentation for the recommended settings? can be run as Windows Services and ASP. Good performance summary for complex solutions/ The project is recommended for use with Superwebsocket because it is a stable and highly configurable library. For simple and fast-developing projects I would choose Fleck, but if there is a way to use the latest Windows server as a test and production machine, it is recommended that you discard the two and choose Signalr.

. NET WebSocket Development Package comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.