Efficient and simple WebSocket service development package Beetle

Source: Internet
Author: User

WebSocket is primarily designed to address the application of flexible communication applications on the web with long connections, but WebSocket itself is only a basic protocol and is not flexible for messages, after all WebSocket only provides the basic data format for text and binary streams. In the actual application is more inclined to the processing of object messages, and on this basis, it is more desirable to integrate a series of message routing mechanism to solve the problem of message processing. In order to solve the above problems beetle a number of high-level packages for websocket, making it easier and more flexible for the server to process messages. The following shows the simplicity of the Beetle WebSocket development package through different examples.

Hello World

This is an example of each development language starter program, which also shows how to use Beetle websocket this component. Implementation of a helloword such a websocket service is a very simple thing, the specific code is as follows

public class Helloserver:baseserver    {        [Command ("Hello")] public        void Onlist (Beetle.Express.IChannel Channel, string ID, string command, Jtoken token)        {            string result = "Hello" +token. ToString ();            Send (Channel,id,command,result);        }    }

is not very simple, this is a simple websocket communication service, the start of the service only need a very simple sentence can

Mchatserver = new Webchatserver ();            Mchatserver.open (9123);

This allows the service to be opened on the 9123 port. What is the next Web call? Beetle also for the WebSocket JS package, so the JS call this service is very simple.

<script type= "Text/javascript" >        var wsuri = "ws://127.0.0.1:9125/";        function init () {            websocket = new WSClient ();            Websocket.onconnect = function (evt) {};            Websocket.onclose = function (evt) {};            websocket.onreceive = function (evt) {};            Websocket.onerror = function (evt) {};            Websocket.connect (Wsuri);            $ (' #cmdconnect '). Click (function () {                websocket.send ("Hello", $ (' #txtName '). Val (), Function (result, error) {                    $ (' #txtResult '). HTML (result);                })        ;} Window.addeventlistener ("Load", init, false);    </script>

after encapsulation is not and traditional processing to be much simpler, the following is the effect of its operation.


a websocket-based Hello World example is done by beetle very simple, but the actual application field is not so simple, the following is a simple data query application scenario through the Beetle WebSocket package.

data Query

the next thing to do is to make a simple data paging query application through the Beetle websocket communication packet.         

public class Dataserver:baseserver    {        [Command ("search]] public        void Search (Beetle.Express.IChannel Channel, string ID, string command, Jtoken token)        {            int size = ten;            int pageindex = token["pageindex"]. Toobject<int> ();            SearchResult result = new SearchResult ();            Result. Pages = mcustomers.count/size;            if (mcustomers.count% Size > 0)                result. pages++;            for (int i = pageindex * size; i < mcustomers.count; i++)            {                result. Items.Add (Mcustomers[i]);                if (result. Items.Count >= size) break                    ;            Send (channel, ID, command, result);        }    }

is the code very simple, and what about JS code?

function Search () {websocket.send ("search", {Pageindex:pageindex}, function (data, error) {                $ (' #lstCustomer '). empty (); if (!pages) {pages = data.                    Pages; Createpagination (data.                                Pages); } for (p = 0; p < data. Items.length; p++) {item = data.                    ITEMS[P];                CreateItem (item);        }            }); } function createpagination (pages) {for (p = 0; p < pages; p++) {$ (' <li><a            href= "javascript:pageindex= ' + P + '; search ()" > ' + (p + 1) + ' </a></li> '). AppendTo ($ (' #pagination ')); }} function CreateItem (item) {var html = ' <tr> ' + ' <td&gt ; ' + Item. CustomerID + ' </td> ' + ' <td> ' + item. CompanyName + ' </td> ' + ' <td> '+ Item. Address + ' </td> ' + ' <td> ' + item. City + ' </td> ' + ' <td> ' + item. Country + ' </td> ' + ' <td> ' + item.            Phone + ' </td> ' + ' </tr> ';        $ (HTML). AppendTo ($ (' #lstCustomer ')); }

the same simple and convenient code will be able to complete a data paging query based on WebSocket

Summary

through the above example you can learn that the development package through Beetle WebSocket can be very efficient in web development websocket communication applications, if you are interested in this communication package can go to Https://github.com/IKende/websocket.samples get more examples (including online chat rooms)

Efficient and simple WebSocket service development package Beetle

Related Article

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.