简体   繁体   English

Websocket 内存泄漏 node.js。 事件发射器?

[英]Websocket memory leak node.js. Event emitters?

I am getting the event emitter leak after using my code 10 times essentially.基本上使用我的代码 10 次后,我得到了事件发射器泄漏。 I understand the default of event emitter auto sending out a warning in the console.我了解事件发射器自动在控制台中发出警告的默认设置。 My question is what in this code is directly creating the event listeners?我的问题是这段代码中直接创建事件侦听器的内容是什么? Is it poor coding on my part or is it just how the websockets are stacked onto each other?是我的编码很差还是只是 websockets 是如何堆叠在一起的?

I'll explain the code a bit.我将解释一下代码。 I have one websocket within another and I figured it would serve the data to a web page essentially flowing from Twitch to a localhost site.我在另一个 websocket 中有一个 websocket,我认为它会将数据提供给一个网页,该网页基本上从 Twitch 流向本地主机站点。 However, if I use the keywords more than 10 times, I get the error.但是,如果我使用关键字超过 10 次,则会出现错误。 I do not understand enough about WebSockets to really understand why my code creates a new listener with each msg.text received so anyone with a bit more understanding please help!我对 WebSockets 的了解不够,无法真正理解为什么我的代码会为每个收到的 msg.text 创建一个新的侦听器,因此请有更多理解的人帮忙!

I believe me issue to be similar to this though I am having a hard time conceptualizing my own code here我相信我的问题与类似,尽管我在这里很难概念化我自己的代码

const { paintballShot } = require('./JavaScript/paintballGunFire');
const { readPin } = require('./JavaScript/readPin');
const ws = require('ws');
const express = require('express');


const app = express();

//CONNECT TO TWITCH
let client = new ChatClient({
  connection: {
    type: "websocket",
    secure: true,
  }
});

//connected?
client.on("ready", () => console.log("Successfully connected to chat"));
client.on("close", (error) => {
  if (error != null) {
    console.error("Client closed due to error", error);
  }
});

//create headless websocket
const wsServer = new ws.Server({ noServer: true });
wsServer.on('connection', function connection(socket) {

//call other websocket connected to Twitch from inside the new websocket
client.on("PRIVMSG", (msg, error) => {
  if (msg.messageText === "right") {
    socket.send(JSON.stringify(`${msg.displayName}: ${msg.messageText}`));
  }
  
  if (msg.messageText === "left") {
    socket.send(JSON.stringify(`${msg.displayName}: ${msg.messageText}`));
  }

  if (msg.messageText === "fire") {
    socket.send(JSON.stringify(`${msg.displayName}: ${msg.messageText}`));
    paintballShot();
  }

  if (msg.messageText === "pin") {
    readPin();
  }
  process.on('uncaughtException', function (err) {
    console.log(err);
}); 
});


client.connect();
client.join("channel");

  socket.on('message', message => console.log(message));
});

// `server` is a vanilla Node.js HTTP server
const server = app.listen(3000);
server.on('upgrade', (request, socket, head) => {
  wsServer.handleUpgrade(request, socket, head, socket => {
    wsServer.emit('connection', socket, request);
  });
});
process.on('uncaughtException', function (err) {
    console.log(err);
});

To wrap this up, the library I am using (Dank TwitchIRC) does have a connection rate limiter that seems to work if you add it to your chat client in the beginning.总结一下,我正在使用的库(Dank TwitchIRC)确实有一个连接速率限制器,如果您一开始将它添加到聊天客户端,它似乎可以工作。 If I set it low enough, depending on the messages received from Twitch, it will end connections just as fast, meaning no memory leak.如果我将它设置得足够低,根据从 Twitch 收到的消息,它会以同样快的速度结束连接,这意味着没有内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM