我使用 socket.io 开发了一个实时文本网络应用程序,它可以在网页上连续显示文本。当我在下面运行我的代码时,多个客户端连接服务器上 V8 javascript 进程的内存使用量继续增长,垃圾收集似乎没有发生。我的代码有什么问题吗?我在某处有内存泄漏吗?
io.sockets.on('connection', function (socket) {
numOfUsers += 1;
var _RttPanes;
var sendText = function (input, callback) {
var data = JSON.parse(input);
socket.emit(data.action, input);
callback();
};
socket.emit('RttConnect', 'connected');
socket.on('RttConnectRequest', function (strJsonMsg) {
//add socket id to json string
var intPosition = strJsonMsg.indexOf("}");
var strFirstPart = strJsonMsg.substring(0, intPosition);
var strNewJson = strFirstPart + ',"socketID":"' + socket.id + '"}';
var payload = {
jsonData: strNewJson,
sendTranscriptToClient: sendText
};
//call ASP.NET code
RttPaneConnect(payload, true);
//RttPaneConnect(payload, function (error, result) {
// if (error) throw error;
// console.log(result);
//});
//Add property to the socket
var message = JSON.parse(strNewJson);
socket.UserName = message.username.toLowerCase();
});
socket.on('error', function (err) {
console.log("socket.io-client 'error'", err);
//reconnect();
});
socket.on('connect_failed', function () {
console.log("socket.io-client 'connect_failed'");
//reconnect();
});
socket.on('disconnect', function () {
console.log("socket.io-client 'disconnect'");
var strNewJson = '{"socketID":"' + socket.id + '"}';
var payload = {
jsonData: strNewJson,
sendTranscriptToClient: sendText
};
//RttPaneDisconnect(payload, true);
RttPaneDisconnect(payload, function (error) { console.log("socket.io-client 'disconnect EXIT'"); });
});
socket.on("LoggInUser", function (logInsocketid, logOffsocektid) {
var clients = io.sockets.clients();
for (i = 0; i < clients.length; i++) {
if (clients[i].id == logInsocketid) {
clients[i].emit("logOffCurrentUserSuccessful", logInsocketid, logOffsocektid);
break;
}
}
});
socket.on('logOffCurrentUser', function (strJsonMsg) {
var data = JSON.parse(strJsonMsg);
var clients = io.sockets.clients();
for(i=0;i<clients.length;i++){
if (clients[i].UserName == data.username) {
if(clients[i].id != socket.id){
console.log("socket.io-client 'logOffCurrentUser' id: " + clients[i].id);
var intPosition = strJsonMsg.indexOf("}");
var strFirstPart = strJsonMsg.substring(0, intPosition);
//add socket id to json string
var strNewJson = strFirstPart + ',"socketID":"' + socket.id + '" , "clientSocketID":"' + clients[i].id + '"}';
var payload = {
jsonData: strNewJson,
sendTranscriptToClient: sendText
};
//disconnect user from GLAS
RttPaneDisconnectFirstAndConnectSecond(payload, function (error, result) {
if (error) {
console.log('Ahh! An Error!');
return;
}
console.log("forceLogOff socket.id, clients[i].id: " + socket.id + " " + clients[i].id);
clients[i].emit("forceLogOff", socket.id, clients[i].id);
});
break;
}
}
}
});
});
这是调用我的 .net 代码以获取实时文本的 edge.js 代码
var RttPaneDisconnect = edge.func({
references: ['System.Data.dll', 'bin\\Newtonsoft.Json.dll', 'bin\\LiveNoteStreamWeb.dll', 'bin\\StreamServerService.dll', 'System.Configuration.dll', 'System.Security.dll'],
source:
function () {/*
using System.Data;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Configuration;
using LiveNoteStreamWeb;
using StreamWeb;
using System.Security.Cryptography;
using System.Text;
using System;
public class Startup
{
public WebRttPane _RttPane;
public async Task<object> Invoke(dynamic data)
{
System.Diagnostics.Debugger.Break();
WebRttPane _WebRttPane = new WebRttPane();
_WebRttPane.StopRtt(data);
return _WebRttPane;
}
}
public class SessionInfo
{
public string username, session, ew, token, socketID;
}
*/
}
});
var RttPaneConnect = edge.func({
references: ['System.Data.dll', 'bin\\Newtonsoft.Json.dll', 'bin\\LiveNoteStreamWeb.dll', 'bin\\StreamServerService.dll', 'System.Configuration.dll', 'System.Security.dll'],
source:
function () {/*
using System.Data;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Configuration;
using LiveNoteStreamWeb;
using StreamWeb;
using System.Security.Cryptography;
using System.Text;
using System;
public class Startup
{
public WebRttPane _RttPane;
public async Task<object> Invoke(dynamic data)
{
//System.Diagnostics.Debugger.Break();
WebRttPane _WebRttPane = new WebRttPane(data);
_WebRttPane.StartRealTime();
return _WebRttPane;
}
}
public class SessionInfo
{
public string username, session, ew, token, socketID;
}
*/
}
});