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