WebSocket in HTML5 3-Communication Model socket. io_javascript skills

Source: Internet
Author: User
Tags emit learnboost
Socket. io can provide programmers with the same programming experience on the client and server. socket. io supports any browser and any Mobile devices. Next, I will explain to you through this article HTML5 WebSocket entry 3-Communication Model socket. io. If you need it, refer to socket. io. Why is it born? See the following description.

Why socket. io?

Node. js provides an efficient server running environment, but the browser supports HTML5 differently. To ensure compatibility with all browsers, it provides excellent real-time user experience, in addition, it provides programmers with the consistent programming experience between the client and the server, so socket. io was born.

Socket. io is designed to support any browser and any Mobile device. Currently, it supports mainstream PC browsers (such as IE, Safari, Chrome, Firefox, and Opera) and Mobile browsers (such as iphone Safari, ipad Safari, android WebKit, and WebOS WebKit ).

Based on node. js, socket. io simplifies WebSocket APIs and unifies various communication APIs. It supports WebSocket, Flash Socket, AJAX long-polling, AJAX multipart streaming, Forever IFrame, and JSONP polling.

Socket. io solves the real-time communication problem and unifies the programming methods between the server and the client. After the socket is started, it is like creating a pipeline between the client and the server. The two sides can communicate with each other.

Install

Run npm install socket. io in the command line to install the SDK.

Server programming model

Server programming is the same as that of a common server. It starts the server, provides services, and handles events.

For example, the following server. js:

var http = require('http')  , url = require('url')  , fs = require('fs')  , server;server = http.createServer(function(req, res){  // your normal server code  var path = url.parse(req.url).pathname;  switch (path){  case '/':   res.writeHead(200, {'Content-Type': 'text/html'});   res.write('Hello! Try the Socket.io Test');   res.end();   break;  case '/index.html':   fs.readFile(__dirname + path, function(err, data){    if (err) return send404(res);    res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'})    res.write(data, 'utf8');    res.end();   });   break;  default: send404(res);  }}),send404 = function(res){  res.writeHead(404);  res.write('404');  res.end();};server.listen(8080);var io = require('socket.io').listen(server);io.sockets.on('connection', function(socket){ console.log("Connection " + socket.id + " accepted."); socket.on('message', function(message){    console.log("Received message: " + message + " - from client " + socket.id); }); socket.on('disconnect', function(){  console.log("Connection " + socket.id + " terminated."); });});

Client Programming Model

Client programming is also similar to processing methods, connecting to the server and interacting with information.

For example, the following index.html page:

   Socket. io Test  
Related Article

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.