如何在服务器重启前关闭节点websocket客户端上的所有websocket连接



我已经在我的节点服务器上创建了一个websocket客户端,它可以监听第三方websocket服务器。客户端服务器之间的通信都很好,但我面临的唯一问题是,当我的服务器重新启动套接字连接代码再次运行,每次新连接与服务器建立,导致太多的打开套接字连接。我想做的是当服务器重新启动关闭所有现有的打开连接,然后建立新的连接。

const WebSocketClient = require('websocket').client;
let sockets = [];
function openSocket(client,id){
client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error 1: ' + error);
});
client.on('connect', function(connection) {
console.log('WebSocket Client Connected');
connection.on('error', function(error) {
console.log("Connection Error 2: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
// setTimeout(run, 1000);
});
connection.on('message', function(message) {
let {utf8Data:socketResData} = message;
// socketResData = JSON.parse(socketResData);
console.log(socketResData);
});
function subscribe(){
if (connection.connected) {
connection.send(JSON.stringify(
{
action: "SubscribeToAuction",
data: id
}
));
}
}
subscribe();

function pingServer() {
if (connection.connected) {
connection.send(JSON.stringify(
{
action: "Ping",
}
));
setTimeout(pingServer, 540000);
}
}
pingServer();
});
client.connect(socketUrl,null,{"x-forwarded-client-id":id},null);
sockets.push(client);
}
function run(){
axios(url, {
method: 'GET',
headers: {
authority,
'x-forwarded-client-id': id,
},
}).then(res => {
const auction = res.data.models.HomePageModel.upcomingModel.upcomingAuctions;
let i = 0;
for(let el of auction){
openSocket(`client${i}`,el.auctionUuid)
i++;
}
}).catch(err => console.log(err))
}
run();
setTimeout(() => {
for(let s in sockets){
console.log(sockets[s]);
sockets[s].close();
}
},10000)

只是为了检查可能性,我尝试将所有连接存储在一个数组中,然后循环它们并试图关闭,但得到这个错误

sockets[s].close();
^
TypeError: sockets[s].close is not a function

我只想知道处理这个用例的最佳方法是什么。如有任何帮助,我将不胜感激。

我发现了我的错误我没有直接push client而是将连接对象添加到数组中现在close()

const WebSocketClient = require('websocket').client;
let sockets = [];
function openSocket(client,id){
client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error 1: ' + error);
});
client.on('connect', function(connection) {
console.log('WebSocket Client Connected');
sockets.push(connection); // websockets close methode is available on socket connection object
connection.on('error', function(error) {
console.log("Connection Error 2: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
// setTimeout(run, 1000);
});
connection.on('message', function(message) {
let {utf8Data:socketResData} = message;
// socketResData = JSON.parse(socketResData);
console.log(socketResData);
});
function subscribe(){
if (connection.connected) {
connection.send(JSON.stringify(
{
action: "SubscribeToAuction",
data: id
}
));
}
}
subscribe();

function pingServer() {
if (connection.connected) {
connection.send(JSON.stringify(
{
action: "Ping",
}
));
setTimeout(pingServer, 540000);
}
}
pingServer();
});
client.connect(socketUrl,null,{"x-forwarded-client-id":id},null);
}
function run(){
axios(url, {
method: 'GET',
headers: {
authority,
'x-forwarded-client-id': id,
},
}).then(res => {
const auction = res.data.models.HomePageModel.upcomingModel.upcomingAuctions;
let i = 0;
for(let el of auction){
openSocket(`client${i}`,el.auctionUuid)
i++;
}
}).catch(err => console.log(err))
}
run();
setTimeout(() => {
for(let s in sockets){
sockets[s].close();
}
},10000)

最新更新