Linux (Ubuntu) node. js, node-websocket-server environment construction and simple chat program

Source: Internet
Author: User
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:

  1. VarWs = require (_ dirname +'/Lib/ws/Server');
  2. // 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
  3. VarServer = ws. createServer ();
  4. Server. addListener ('Connection',Function(Conn ){// Check whether a new connection exists in the listener.
  5. Console. log ('Connection ....');
  6. // Conns. push (conn );
  7. Conn. addListener ('Message',Function(Msg ){
  8. // Check whether new messages have arrived.
  9. Console. log (msg );
  10. /* For (var I = 0; I < span> <>
  11. If (conns [I]! = Conn ){ 
  12. Conns [I]. send (msg ); 
  13. } 
  14. }*/
  1. // Send Broadcast
  2. Server. broadcast (msg );
  3. });
  4. });
  5. // Set the listening port
  6. Server. listen (8000,"127.0.0.1");
  7. Console. log ('Running ......');
Save the file and run node server. js. If running... is displayed, the server is running.

Write a client below

  1. <Html>
  2. <Head>
  3. <Script Type="Text/javascript">
  4. VarHost='2017. 0.0.1';
  5. VarPort=8000;
  6. VarUrl='Ws ://'+ Host + ':' + port + '/';
  7. VarW=NewWebSocket (url );
  8. VarAudioElement=Document. CreateElement ('audio ');
  9. AudioElement. setAttribute ('src', 'qqmsg.mp3 ');
  10. W. onopen=Function(){
  11. Console. log ("Open ");
 
  1. // Callback during connection
  2. $ ('Chat-box '). InnerHTML='Connect to server ......
    '
    ;
  3. }
  4. W. onmessage=Function(E ){
  5. VarMsg=E. Data;
  6. VarChatBox= $ ('Chat-box ');
  7. // Called when a new message is received
  8. AudioElement. play ();
  9. ChatBoxChatBox. innerHTML= ChatBox. innerHTML + msg +'<Br/>';
  10. }
  11. Function send (){
  12. VarTalk= $ ('Tal ');
  13. VarNike= $ ('Nike ');
  14. // Click "Send message"
  15. W. send ('<Strong Style="Color: red">'+ Nike. value + ': Strong>'+ Talk. value );
  16. }
  17. Function $ (id ){
  18. Return document. getElementById (id );
  19. }
  20. Script>
  21. Head>
  22. <Body>
  23. <Div Id="Chat-box" Style="Bordddder: 1px solid # cccccc; width: 400px; height: 400px; overflow: scroll ;"> Div>
  24. Name:<Input Type="Text" Id="Nike"/><Br/>
  25. Content:<Input Type="Text" Id="Talk"/><Input Type="Button" Id="Send" OnClick="Send ();" Value="Send"/>
  26. Body>
  27. Html>
Save the file and open it in a browser. Effect Creation

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.