我正试图用电子和Node.js下载一条流,使用WebTorrent。好吧,这是我在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()增加限制
发布于 2018-06-25 02:37:46
只是个警告!
根据Nodejs.org文档
默认情况下,如果为特定事件添加了10多个侦听器,EventEmitters将打印警告。这是一个有用的默认设置,可以帮助查找内存泄漏。显然,并非所有事件都应仅限于10个侦听器。emitter.setMaxListeners()方法允许为这个特定的EventEmitter实例修改限制。可以将该值设置为Infinity (或0),以指示无限数量的侦听器。
因此,您需要在代码中添加一行代码
"emitter.setMaxListeners(n)“
https://stackoverflow.com/questions/51015181
复制相似问题