Helloworld of socket. Io

Source: Internet
Author: User
Tags emit

As mentioned earlier, websocket has some client frameworks to simplify programming. Today, we use socket. Io and node. js to write a helloworld websocket communication.
One of them is socket. io, socket. io belongs to node. JS (nodejs is a server-side Programming Technology JavaScript) module, node. JS can use socket. the I/O Library easily implements the websocket function.
Several knowledge points to be aware:
Javascript can be used as a language for server-side programming. The typical case is node. js.
Node. js uses event-driven, asynchronous programming to design http://baike.baidu.com/view/3974030.htm for Network Services
Node. js needs to execute the service environment, can download on the official website, I use http://nodejs.org support window
Run the command: node XXX. js will execute the XXX. js script.
Socket. Io official website http://socket.io/and node. js with the use
The socket. Io module is not included in node. js. Therefore, to install it, run the NPM install socket. Io command. If a module cannot be found during running, use this command to install it.

Node. js's first helloworld
1. Write a JS file helloworld. js
VaR HTTP = require ('http ');
HTTP. createserver (function (req, Res ){
Res. writehead (200, {'content-type': 'text/plain '});
Res. End ('Hello world \ n ');
}
). Listen (8080, '192. 0.0.1 ');
Console. Log ('server running at http: // 127.0.0.1: 8080 /');
2. Run the node helloworld. js command under the JS directory in the CMD script.
The message "server running at http: // 127.0.0.1: 8080/" is printed/
3. Enter http: // 127.0.0.1: 8080 in the browser. The helloworld page is displayed.

The first example of using Socket. Io to implement the websocket Function
1. Write a helloworld.html File
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Title> new document </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<SCRIPT src = "/socket. IO/socket. Io. js"> </SCRIPT> <! -- Obtain socket. Io. js from the server as long as your server has installed the socket. Io module. -->
<SCRIPT>
VaR socket = Io. Connect ('ws: // 127.0.0.1/'); // establish a connection to the server
Socket. emit ('client', 'Hello world! '); // Send data to the server. on the server side, you can receive data whose name is client.
Socket. On ('server', function (data) {// receives data from the server named Server
Alert (data );
});
</SCRIPT>
<Body>
</Body>
</Html>
2. Write a script helloworld_server.js next to it.
VaR Server = require ('HTTP '). createserver (handler); // use handler to process requests
VaR IO = require ('socket. Io '). Listen (server); // socket. Io object
VaR FS = require ('fs'); // The object used to operate the file
Server. Listen (80); // listen to port 80 of the Local Machine

// Function used to process the request
Function handler (req, Res) {// req indicates the request object res indicates the response object
// Nvidirnameis the application root directory, which reads the helloworld.html file in the root directory and sends the response
FS. readfile (_ dirname + '/helloworld.html ',
Function (ERR, data ){
If (ERR ){
// If an exception occurs, send an error message to the client.
Res. writehead (500 );
Return res. End ('error loading index.html ');
}
Res. writehead (200 );
Res. End (data); // send the content in the file and end the response.
});
 
}

// Listener
Io. sockets. On ('connection', function (socket ){
Console. Log ('Connection success ');
// Receive data from the client named Client
Socket. On ('client', function (data ){
Console. Log ('Received message from client: '+ data );
Socket. emit ('server', 'I have Ed you message:' + data); // send data named server to the client
});
});

3. Start the server: node helloworld_server.js
4. Enter http: // 127.0.0.1 in the browser
The browser will send a message to the server, and the server will reply a message to the browser.

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.