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