Socket. IO-build cross-browser real-time applications based on WebSocket

Source: Internet
Author: User

Socket. IO is a powerful framework that helps you build real-time applications across browsers Based on WebSocket. Supports mainstream browsers, multiple platforms, and multiple transmission modes. You can also combine the Exppress framework to build complex real-time applications with various functions.

Articles you may be interested in
  • Chance-powerful JavaScript random number generation class library
  • Manifesto-HTML5 Offline Application cache verification tool
  • Codrops Tutorial: full-screen webpage transition effect based on CSS3
  • Zoom. js: A unique page content scaling plug-in
  • Popline: a very special floating HTML5 text editor Toolbar

 

1. Use the Node HTTP Server

Server Sample Code:

var app = require('http').createServer(handler)  , io = require('socket.io').listen(app)  , fs = require('fs')app.listen(80);function handler (req, res) {  fs.readFile(__dirname + '/index.html',  function (err, data) {    if (err) {      res.writeHead(500);      return res.end('Error loading index.html');    }    res.writeHead(200);    res.end(data);  });}io.sockets.on('connection', function (socket) {  socket.emit('news', { hello: 'world' });  socket.on('my other event', function (data) {    console.log(data);  });});

Client sample code:

<script src="/socket.io/socket.io.js"></script><script>  var socket = io.connect('http://localhost');  socket.on('news', function (data) {    console.log(data);    socket.emit('my other event', { my: 'data' });  });</script>

2. Use the Express 3 framework

Server Sample Code:

var app = require('express')()  , server = require('http').createServer(app)  , io = require('socket.io').listen(server);server.listen(80);app.get('/', function (req, res) {  res.sendfile(__dirname + '/index.html');});io.sockets.on('connection', function (socket) {  socket.emit('news', { hello: 'world' });  socket.on('my other event', function (data) {    console.log(data);  });});

Client sample code:

<script src="/socket.io/socket.io.js"></script><script>  var socket = io.connect('http://localhost');  socket.on('news', function (data) {    console.log(data);    socket.emit('my other event', { my: 'data' });  });</script> 
Supported Transmission

In order to provide real-time connections to different browsers, the Socket. IO option will automatically and most competent transport mode at runtime, without affecting the use of APIs.

  • WebSocket
  • Adobe Flash Socket
  • AJAX long polling
  • AJAX multi-stream
  • Iframe
  • JSONP round robin
Supported browsers and desktops
  • Internet Explorer 5.5 +
  • Safari 3 +
  • Google Chrome 4 +
  • Firefox 3 +
  • Opera 10.61 +
Mobile Terminal
  • IPhone Safari
  • IPad Safari
  • Android WebKit
  • WebOs WebKit

 

 

Articles you may be interested in
  • OverAPI.com-the most comprehensive Online quick query manual for developers in history
  • CSS Matic: The ultimate CSS toolbox essential to web designers
  • Front-end digest-practical tools and tips for optimizing Web Images
  • Debuggex-Excellent visual debugging tool for Regular Expression
  • Superhero: resources for building large JavaScript applications

 

Link to this article: Socket. IO-build a real-time WebSocket-based application

Source: Dream sky ◆ focus on front-end development technology ◆ share web design resources

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.