tracker: function (cb) { var tracker = new TrackerServer( serverType === 'udp' ? { http: false } : { udp: false } ) tracker.on('error', function (err) { t.fail(err) }) tracker.on('start', function () { trackerStartCount += 1 }) tracker.listen(function (port) { var announceUrl = serverType === 'http' ? 'http://127.0.0.1:' + port + '/announce' : 'udp://127.0.0.1:' + port // Overwrite announce with our local tracker leavesParsed.announce = [ announceUrl ] leavesParsed.announceList = [[ announceUrl ]] cb(null, tracker) }) },
tracker: function (cb) { var tracker = new TrackerServer('udp') tracker.on('error', function (err) { t.fail(err) }) tracker.on('start', function () { trackerStartCount += 1 }) tracker.listen(function (port) { var announceUrl = 'udp://127.0.0.1:' + port leavesParsed.announce = [ announceUrl ] leavesParsed.announceList = [[ announceUrl ]] magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash + '&tr=' + encodeURIComponent(announceUrl) cb(null, tracker) }) },
function magnetDownloadTest (t, serverType) { t.plan(10) var tracker = new TrackerServer( serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } ) tracker.on('error', function (err) { t.fail(err) }) tracker.on('warning', function (err) { t.fail(err) }) var trackerStartCount = 0 tracker.on('start', function () { trackerStartCount += 1 }) var parsedTorrent = extend(fixtures.leaves.parsedTorrent) var magnetURI, client1, client2 series([ function (cb) { tracker.listen(cb) }, function (cb) { var port = tracker[serverType].address().port var announceUrl = serverType === 'http' ? 'http://127.0.0.1:' + port + '/announce' : 'udp://127.0.0.1:' + port parsedTorrent.announce = [ announceUrl ] magnetURI = 'magnet:?xt=urn:btih:' + parsedTorrent.infoHash + '&tr=' + encodeURIComponent(announceUrl) client1 = new WebTorrent({ dht: false }) client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) client1.on('torrent', function (torrent) { // torrent metadata has been fetched -- sanity check it t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') var names = [ 'Leaves of Grass by Walt Whitman.epub' ] torrent.once('noPeers', function (announceType) { t.equal(announceType, 'tracker', 'noPeers event seen with correct announceType') }) t.deepEqual(torrent.files.map(function (file) { return file.name }), names) torrent.load(fs.createReadStream(fixtures.leaves.contentPath), function (err) { cb(err) }) }) client1.add(parsedTorrent, {store: MemoryChunkStore}) }, function (cb) { client2 = new WebTorrent({ dht: false }) client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) client2.on('torrent', function (torrent) { torrent.files.forEach(function (file) { file.getBuffer(function (err, buf) { if (err) throw err t.deepEqual(buf, fixtures.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() }) }) torrent.once('done', function () { t.pass('client2 downloaded torrent from client1') torrentDone = true maybeDone() }) var gotBuffer = false var torrentDone = false function maybeDone () { if (gotBuffer && torrentDone) cb(null) } }) client2.add(magnetURI, {store: MemoryChunkStore}) } ], function (err) { t.error(err) t.equal(trackerStartCount, 2) tracker.close(function () { t.pass('tracker closed') }) client1.destroy(function (err) { t.error(err, 'client1 destroyed') }) client2.destroy(function (err) { t.error(err, 'client2 destroyed') }) }) }
/** * Created by Administrator on 2016/1/21. */ var Tracker=require('bittorrent-tracker/server'); var server=new Tracker({ udp:false, http:false, ws:true }); server.listen(9090,function(){ console.log("we strat listen"); }); server.on('listening',function(){ console.log("start listen requests on",server.http.address()); }); server.on('start',function(peerdId,params){ console.log("start service:peerId is"+peerdId+"params"+params); }); server.on('complete',function(peerdId,params){ console.log("complete service:peerId is"+peerdId+"params"+params); }); server.on('update',function(peerdId,params){ console.log("update service:peerId is"+peerdId+"params"+params); }); server.on('stop',function(peerdId,params){ console.log("stop service:peerId is"+peerdId+"params"+params); }); server.ws.on("connection",function(socket){ console.log("connect:",socket);
test('client.seed: stream', function (t) { t.plan(9) var tracker = new Tracker({ udp: false, ws: false }) tracker.on('error', function (err) { t.fail(err) }) tracker.on('warning', function (err) { t.fail(err) }) var seeder, client, announceUrl, magnetURI series([ function (cb) { tracker.listen(cb) }, function (cb) { var port = tracker.http.address().port announceUrl = 'http://localhost:' + port + '/announce' seeder = new WebTorrent({ dht: false }) seeder.on('error', function (err) { t.fail(err) }) seeder.on('warning', function (err) { t.fail(err) }) var stream = new Readable() stream._read = function () {} stream.push('HELLO WORLD\n') stream.push(null) var seederOpts = { name: 'hello.txt', pieceLength: 5, announce: [ announceUrl ] } seeder.seed([stream], seederOpts, function (torrent) { magnetURI = torrent.magnetURI cb(null) }) }, function (cb) { client = new WebTorrent({ dht: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) client.add(magnetURI, function (dl) { t.equal(dl.files.length, 1) t.equal(dl.files[0].name, 'hello.txt') t.equal(dl.files[0].length, 12) dl.files[0].getBuffer(function (err, buf) { t.error(err) t.equal(buf.toString('utf8'), 'HELLO WORLD\n', 'content') cb(null) }) }) } ], function (err) { t.error(err) seeder.destroy(function (err) { t.error(err, 'seeder destroyed') }) client.destroy(function (err) { t.error(err, 'client destroyed') }) tracker.close(function () { t.pass('tracker closed') }) }) })