engine.io是一個socket.io的抽象實現,做爲socket.io的服務器和瀏覽器之間交換的數據的傳輸層。它不會取代Socket.IO,它只是抽象出固有的複雜性,支持多種瀏覽器,設備和網絡的實時數據交換。Server (A) Listening on a porthtml
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/1384093598133socket