The first step is to build node. js. node. js is a javascript parser. For details about node. js, refer
The first step is to build node. js. node. js is a javascript parser.
Node. js read: http://www.linuxidc.com/Linux/2011-11/46735.htm
Download node.. js. decompress the zip package, run the command line cd to change the directory, and install the command $. /configure $ sudo make install if you do not know the two commands, continue google. In Windows, it seems that installation is troublesome. After installation, if $ node -- version is displayed as v0.6.2-pre, it indicates that the installation is successful. The version number may be different.
If you install the SDK as mentioned above, no other configurations are needed. Try to parse a js file,
Create a test. the js content is in one sentence consol. log ("Hello Node. js "); Save and then execute node test. if JavaScript displays Hello Node. js instructions can be correctly run.
Next, install node-websocket-sever to download the source code here. Note that it is very boring, and websocket has not become a standard, and it is constantly being corrected, chrome 14 uses drafts 10, which makes drafts 75 and 76 unusable. The connection version above supports dratf 10 and can be run on chrome 14 or later. The downloaded file is a library without installation. Use this library.
The server. js file is as follows:
- VarWs = require (_ dirname +'/Lib/ws/Server');
- // Include the following library _ dirname: indicates that the current directory must contain the library you want to use no matter where your file is stored, here I include the server under lib/ws. js
- VarServer = ws. createServer ();
- Server. addListener ('Connection',Function(Conn ){// Check whether a new connection exists in the listener.
- Console. log ('Connection ....');
- // Conns. push (conn );
- Conn. addListener ('Message',Function(Msg ){
- // Check whether new messages have arrived.
- Console. log (msg );
- /* For (var I = 0; I < span> <>
- If (conns [I]! = Conn ){
- Conns [I]. send (msg );
- }
- }*/
- // Send Broadcast
- Server. broadcast (msg );
- });
- });
- // Set the listening port
- Server. listen (8000,"127.0.0.1");
- Console. log ('Running ......');
Save the file and run node server. js. If running... is displayed, the server is running.
Write a client below
- <Html>
- <Head>
- <Script Type="Text/javascript">
- VarHost='2017. 0.0.1';
- VarPort=8000;
- VarUrl='Ws ://'+ Host + ':' + port + '/';
- VarW=NewWebSocket (url );
- VarAudioElement=Document. CreateElement ('audio ');
- AudioElement. setAttribute ('src', 'qqmsg.mp3 ');
- W. onopen=Function(){
- Console. log ("Open ");
- // Callback during connection
- $ ('Chat-box '). InnerHTML='Connect to server ......
';
- }
- W. onmessage=Function(E ){
- VarMsg=E. Data;
- VarChatBox= $ ('Chat-box ');
- // Called when a new message is received
- AudioElement. play ();
- ChatBoxChatBox. innerHTML= ChatBox. innerHTML + msg +'<Br/>';
- }
- Function send (){
- VarTalk= $ ('Tal ');
- VarNike= $ ('Nike ');
- // Click "Send message"
- W. send ('<Strong Style="Color: red">'+ Nike. value + ': Strong>'+ Talk. value );
- }
- Function $ (id ){
- Return document. getElementById (id );
- }
- Script>
- Head>
- <Body>
- <Div Id="Chat-box" Style="Bordddder: 1px solid # cccccc; width: 400px; height: 400px; overflow: scroll ;"> Div>
- Name:<Input Type="Text" Id="Nike"/><Br/>
- Content:<Input Type="Text" Id="Talk"/><Input Type="Button" Id="Send" OnClick="Send ();" Value="Send"/>
- Body>
- Html>
Save the file and open it in a browser. Effect Creation