当前位置: 首页 > 工具软件 > engine.io > 使用案例 >

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

段干弘扬
2023-12-01

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

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

(B) Intercepting requests for a http.Server

1var engine = require('engine.io')
2  , http = require('http').createServer().listen(3000)
3  , server = engine.attach(http)
4 
5server.on('connection', function (socket) {
6  socket.on('message', function () { });
7  socket.on('close', function () { });
8});

(C) Passing in requests

01var engine = require('engine.io')
02  , server = new engine.Server()
03 
04server.on('connection', function (socket) {
05  socket.send('hi');
06});
07 
08// …
09httpServer.on('upgrade', function (req, socket, head) {
10  server.handleUpgrade(req, socket, head);
11});
12httpServer.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/1384093598133

转载于:https://www.cnblogs.com/aibo/p/3438533.html

 类似资料: