engine.io是一个socket.io的抽象实现,作为socket.io的服务器和浏览器之间交换的数据的传输层。它不会取代Socket.IO,它只是抽象出固有的复杂性,支持多种浏览器,设备和网络的实时数据交换。Server (A) Listening on a port
1 | var engine = require( 'engine.io' ) |
2 | , server = engine.listen(80) |
4 | server.on( 'connection' , function (socket) { |
5 | socket.send( 'utf 8 string' ); |
(B) Intercepting requests for a http.Server
1 | var engine = require( 'engine.io' ) |
2 | , http = require( 'http' ).createServer().listen(3000) |
3 | , server = engine.attach(http) |
5 | server.on( 'connection' , function (socket) { |
6 | socket.on( 'message' , function () { }); |
7 | socket.on( 'close' , function () { }); |
(C) Passing in requests
01 | var engine = require( 'engine.io' ) |
02 | , server = new engine.Server() |
04 | server.on( 'connection' , function (socket) { |
09 | httpServer.on( 'upgrade' , function (req, socket, head) { |
10 | server.handleUpgrade(req, socket, head); |
12 | httpServer.on( 'request' , function (req, res) { |
13 | server.handleRequest(req, res); |
Client
1 | <script src= "/path/to/engine.io.js" ></script> |
3 | var socket = new eio.Socket( 'ws://localhost/' ); |
4 | socket.on( 'open' , function () { |
5 | socket.on( 'message' , function (data) { }); |
6 | socket.on( 'close' , function () { }); |
项目主页:http://www.open-open.com/lib/view/home/1384093598133