socket.io的抽象实现:engine.io

jopen 11年前

engine.io是一个socket.io的抽象实现,作为socket.io的服务器和浏览器之间交换的数据的传输层。它不会取代Socket.IO,它只是抽象出固有的复杂性,支持多种浏览器,设备和网络的实时数据交换。Server
(A) Listening on a port

var engine = require('engine.io')    , server = engine.listen(80)    server.on('connection', function (socket) {    socket.send('utf 8 string');  });


(B) Intercepting requests for a http.Server

var engine = require('engine.io')    , http = require('http').createServer().listen(3000)    , server = engine.attach(http)    server.on('connection', function (socket) {    socket.on('message', function () { });    socket.on('close', function () { });  });


(C) Passing in requests

var engine = require('engine.io')    , server = new engine.Server()    server.on('connection', function (socket) {    socket.send('hi');  });    // …  httpServer.on('upgrade', function (req, socket, head) {    server.handleUpgrade(req, socket, head);  });  httpServer.on('request', function (req, res) {    server.handleRequest(req, res);  });

Client

<script src="/path/to/engine.io.js"></script>  <script>    var socket = new eio.Socket('ws://localhost/');    socket.on('open', function () {      socket.on('message', function (data) { });      socket.on('close', function () { });    });  </script>

项目主页:http://www.open-open.com/lib/view/home/1384093598133