0

我正在尝试使用WebTorrent下载带有 Electron 和 Node.js 的种子。好吧,这是我在 main.js 中的代码

const electron = require('electron')
const { app, BrowserWindow } = electron

const path = require('path')
const url  = require('url')
const server = require('./server')

let win

function createWindow() {
  win = new BrowserWindow ({ vibrancy: 'dark', width: 400, height: 600, frame: false, resizable: false, transparent: true })
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file',
    slashes: true
  }))
}

app.on('ready', createWindow)

我在 server.js 中的代码是:

require('http').createServer(function (req, res) {
  var WebTorrent = require('webtorrent-hybrid')

  var client = new WebTorrent()

  var magnetURI = 'magnet:?xt=urn:btih:EF3B95AEF1C94FC8E98825386C3B12560FE21CFF&tr=udp://glotorrents.pw:6969/announce&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://torrent.gresille.org:80/announce&tr=udp://tracker.openbittorrent.com:80&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://p4p.arenabg.ch:1337&tr=udp://tracker.internetwarriors.net:1337'

  client.add(magnetURI, { path: 'movies' }, function (torrent) {
    torrent.on('done', function () {
      console.log('torrent download finished')
    })
  })

  res.end('Hello from server started by Electron app!');

}).listen(9000)

当我运行应用程序并在控制台上显示此消息时,问题就开始了:

(节点:9032) MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个准备好的听众。使用emitter.setMaxListeners() 增加限制

4

1 回答 1

0

只是一个警告!!!

根据 Nodejs.org 文档

https://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n

默认情况下,如果为特定事件添加了超过 10 个侦听器,EventEmitters 将打印警告。这是一个有用的默认值,有助于发现内存泄漏。显然,并非所有事件都应仅限于 10 个侦听器。emitter.setMaxListeners() 方法允许为这个特定的 EventEmitter 实例修改限制。该值可以设置为 Infinity(或 0)以指示无限数量的侦听器。

因此,您需要在代码中添加以下行

“发射器.setMaxListeners(n)”

于 2018-06-25T02:37:46.503 回答