直接上代码吧,服务器端:
var express=require('express');
var app=express();
var net=require('net');
var http=require('http').createServer(app);
var io=require('socket.io').listen(http);
var net=require('net');
var nodeServer = new net.Socket();
var aSocket=null;
io.sockets.on('connection', function (socketIO) {
aSocket=socketIO;
};
nodeServer.on('data', function(data) {
if(aSocket!=null){
aSocket.emit('pushToWebClient',useData);
//客户端断开连接
aSocket.on('disconnect', function () {
console.log('DISCONNECTED FROM CLIENT');
});
}
客户端:
socket.on('pushToWebClient', function (useData) {
});
代码截取了部分,过程是:从一个C++写的服务器接受数据,socket.io转发数据给客户端,然后显示到页面上。
接受一会,如果关闭网页,就会出现一堆
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
...(此处省略无数行)
我估计是每次io建立了很多次连接,要怎么才能阻止建立这么多呢?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…