Simple implementation of a websocket Service

Source: Internet
Author: User

Websocket is a protocol mechanism provided in HTML5 to facilitate TCP persistent connection communication. With the popularization of HTML5, this technology will become more and more widely used. Because beetle can flexibly expand different protocols, naturally, websocket support is quite simple. for more information about websocket protocol analysis and processing, see download.ProgramComplete protocol package availableCode, If you want to learn more about the websocket protocol can be here: http://datatracker.ietf.org/doc/rfc6455? Include_text = 1

The following describes how to use beetle to conveniently implement a websocket-based Hello wold program.

Server code
Class program: websocketserver {static void main (string [] ARGs) {tcputils. setup ("beetle"); program server = new program (); server. open( 8088); console. writeline ("websocket start @ 8088"); system. threading. thread. sleep (-1);} protected override void onconnected (Object sender, channeleventargs e) {base. onconnected (sender, e);} protected override void ondisposed (Object sender, channeldisposedeventargs e) {base. ondisposed (sender, e);} protected override void onerror (Object sender, channelerroreventargs e) {base. onerror (sender, e);} protected override void onwebsocketreceive (tcpchannel channel, datapackage e) {console. writeline ("messagetype: {0}", E. type); console. writeline ("requestpath: {0}", E. requestpath); If (E. type = packagetype. text) {console. writeline (E. tostring (); datapackage dp = new datapackage (); DP. type = packagetype. text; DP. data. encoding ("hello" + E. tostring (), encoding. utf8); channel. send (DP );}}}

The above is a complete websocket service. The code is very simple. websocketserver can simply rewrite onwebsocketreceive and process the corresponding datapackage as needed in the event. For the structure and type of datapackage, see download code.

HTML code
<HTML> <meta charset = "UTF-8"/> <title> websocket test </title> <script language = "JavaScript" type = "text/JavaScript"> var wsuri =" WS: // 127.0.0.1: 8088/"; var output; function Init () {output = document. getelementbyid ("output"); testwebsocket ();} function testwebsocket () {websocket = new websocket (wsuri); websocket. onopen = function (EVT) {onopen (EVT)}; websocket. onclose = function (EVT) {onclose (EVT)}; websocket. onmessage = function (EVT) {onmessage (EVT)}; websocket. onerror = function (EVT) {onerror (EVT) };} function onopen (EVT) {writetoscreen ("connected");} function onclose (EVT) {writetoscreen ("disconnected");} function onmessage (EVT) {document. getelementbyid ('result '). value = EVT. data;} function onerror (EVT) {writetoscreen ('<span style = "color: red;"> error: </span>' + EVT. data);} function dosend (Message) {websocket. send (Message);} function writetoscreen (Message) {var pre = document. createelement ("p"); pre. style. wordwrap = "break-word"; pre. innerhtml = message; output. insertbefore (pre);} function onconnect () {wsuri = document. getelementbyid ("url "). value; Init () ;}</SCRIPT> <body> <fieldset> <legend> websocket Hello wold </legend> <p> <span> server URL: </span> <input id = "url" type = "text" value = "WS: // 127.0.0.1: 8088/"/> <input type =" button "value =" connected "onclick =" onconnect () "/> </P> <p> <span> enter you name: </span> <input id = "youname" type = "text" value = "test"/> <input type = "button" value = "Submit" onclick = "dosend (document. getelementbyid ('youname '). value) "/> </P> <p> <span> result: </span> <input id = "result" type = "text" value = ""/> </P> </fieldset> <Div id = "output"> </ div> </body> 
Running Effect

So a hello Wold-based websocket handler will be completed. If you are interested in websocket, you can downloadSource codeUnderstand the principles of websocket protocol analysis, and how to analyze the corresponding websocket protocol from byte.

Download Code

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.