socket.io的抽象實現:engine.io

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)
3  
4 server.on('connection', function (socket) {
5   socket.send('utf 8 string');
6 });

(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)
4  
5 server.on('connection', function (socket) {
6   socket.on('message', function () { });
7   socket.on('close', function () { });
8 });

(C) Passing in requests服務器

01 var engine = require('engine.io')
02   , server = new engine.Server()
03  
04 server.on('connection', function (socket) {
05   socket.send('hi');
06 });
07  
08 // …
09 httpServer.on('upgrade', function (req, socket, head) {
10   server.handleUpgrade(req, socket, head);
11 });
12 httpServer.on('request', function (req, res) {
13   server.handleRequest(req, res);
14 });

Client網絡

1 <script src="/path/to/engine.io.js"></script>
2 <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 () { });
7   });
8 </script>

項目主頁:http://www.open-open.com/lib/view/home/1384093598133socket

相關文章
相關標籤/搜索