NODE+WS Module Implementation WebSocket

Source: Internet
Author: User

First to spit groove, want to find some technical article really TM is not easy, all is JB copy paste, also pretend original. Searching for a WS implementation WebSocket is all the same. A word has not changed, what can I say. Finally think of the search WS module Githup actually the first two pages are not, also those repeated articles, inability to spit groove. In the previous githup (although all English, but cliffs than those copied paste good) https://github.com/websockets/ws#broadcast-example

Let's take a look at my results: Sync on Google and Firefox on the Websocet link.

Client 1

Client 2

Client 1

First put my background code (NODEJS):

Need to install Express module, WS module

1 varExpress = require (' Express ');2 varHTTP = require (' http ');3 varWebSocket = require (' WS ');4 5 varApp =Express ();6 App.use (express.static (__dirname));7 8 varServer =Http.createserver (app);9 varWSS =Newwebsocket.server ({Server});Ten  OneWss.on (' Connection ',functionConnection (WS) { AConsole.log (' Link succeeded! ‘); -Ws.on (' message ',functionIncoming (data) { -         /** the * Send messages to all clients - * wss.clients get all linked clients -          */ -Wss.clients.forEach (functionEach (client) { + client.send (data); -         }); +     }); A }); at  -Server.listen (8000,functionlistening () { -Console.log (' Server started successfully! ‘); -});

Client code:

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <Metaname= "Viewport"6 content= "Width=device-width, User-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">7     <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">8     <title>Online chat</title>9 </Head>Ten <Body> One <inputtype= "text"onblur= "Wsserver.onopen (this.value)"> A <Script> -  varWSServer= NewWebSocket ('ws://127.0.0.1:8000'); - Wsserver.onopen= function(e) { the      (typeofe== 'string') &&Wsserver.send (e);//send data to the background -  }; - Wsserver.onclose= function(e) {//triggered when the link is closed -  +  }; - Wsserver.onmessage= function(e) {//triggered when a message is returned in the background + Console.log (e); A  }; at Wsserver.onerror= function(e) {//error Condition Trigger -  -  } - </Script> - </Body> - </HTML>

The writing is very simple, anyway can run on the line. More content click: meetqy

NODE+WS Module Implementation WebSocket

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.