socket.io的抽象实现:engine.io
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>
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!