diff --git a/node/jlibtorrent.js b/node/jlibtorrent.js index 899d7b567..aec5f51eb 100644 --- a/node/jlibtorrent.js +++ b/node/jlibtorrent.js @@ -1,4 +1,7 @@ -var swig = require('./jlibtorrent.node'); +const EventEmitter = require('events'); +const util = require('util'); + +const swig = require('./jlibtorrent.node'); // swig (function () { @@ -111,7 +114,7 @@ var swig = require('./jlibtorrent.node'); return new swig.session(sp); } - function alertsLoop(s) { + function alertsLoop(emitter, s) { var session_alerts_loop = function () { var max_wait = swig.to_milliseconds(100); var alert = s.wait_for_alert(max_wait); @@ -122,7 +125,7 @@ var swig = require('./jlibtorrent.node'); var size = vector.size(); for (var i = 0; i < size; i++) { var a = vector.get(i); - console.log(a.type() + " - " + a.what() + " - " + a.message()); + emitter.emit('alert', a); } vector.clear(); } @@ -132,14 +135,18 @@ var swig = require('./jlibtorrent.node'); } function Session(settings, logging) { + EventEmitter.call(this); + settings = settings || new exports.SettingsPack(); logging = logging || false; this.s = createSession(settings, logging); - alertsLoop(this.s); + alertsLoop(this, this.s); } + util.inherits(Session, EventEmitter); + Session.prototype.swig = function () { return this.sp; } diff --git a/node/session_test.js b/node/session_test.js index 03cf74395..8dfaf59bb 100644 --- a/node/session_test.js +++ b/node/session_test.js @@ -1,9 +1,13 @@ -var jlibtorrent = require('./jlibtorrent.js'); +const jlibtorrent = require('./jlibtorrent.js'); console.log("Using libtorrent version: " + jlibtorrent.LibTorrent.fullVersion()); -var sp = new jlibtorrent.SettingsPack(); -var s = new jlibtorrent.Session(sp, false); +const sp = new jlibtorrent.SettingsPack(); +const s = new jlibtorrent.Session(sp, false); + +s.on('alert', function (a) { + console.log(a.type() + " - " + a.what() + " - " + a.message()); +}); process.stdout.write('Press ENTER to exit...'); process.stdin.once('data', function (data) {