From 777d6c2225a5fae5f514a141ea3ec25d97763754 Mon Sep 17 00:00:00 2001 From: Joseph Dykstra Date: Fri, 16 Jan 2015 16:45:13 -0600 Subject: Added ./examples/ directory --- examples/browser-download.js | 22 ++++++++++++++++++++++ examples/browser-seed.js | 12 ++++++++++++ examples/browser-stream-to-audio.js | 19 +++++++++++++++++++ examples/browser-stream-to-video.js | 19 +++++++++++++++++++ examples/node-save-to-file-system.js | 16 ++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 examples/browser-download.js create mode 100644 examples/browser-seed.js create mode 100644 examples/browser-stream-to-audio.js create mode 100644 examples/browser-stream-to-video.js create mode 100644 examples/node-save-to-file-system.js (limited to 'examples') diff --git a/examples/browser-download.js b/examples/browser-download.js new file mode 100644 index 0000000..3428f72 --- /dev/null +++ b/examples/browser-download.js @@ -0,0 +1,22 @@ +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +client.add(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + torrent.files.forEach(function (file) { + // Get a url for each file + file.getBlobURL(function (err, url) { + if (err) throw err + + // Add a link to the page + var a = document.createElement('a') + a.download = file.name + a.href = url + a.textContent = 'Download ' + file.name + document.body.appendChild(a) + }) + }) +}) diff --git a/examples/browser-seed.js b/examples/browser-seed.js new file mode 100644 index 0000000..6a6d498 --- /dev/null +++ b/examples/browser-seed.js @@ -0,0 +1,12 @@ +var dragDrop = require('drag-drop/buffer') +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +// When user drops files on the browser, create a new torrent and start seeding it! +dragDrop('body', function (files) { + client.seed(files, function onTorrent (torrent) { + // Client is seeding the file! + console.log('Torrent info hash:', torrent.infoHash) + }) +}) diff --git a/examples/browser-stream-to-audio.js b/examples/browser-stream-to-audio.js new file mode 100644 index 0000000..84dfdfd --- /dev/null +++ b/examples/browser-stream-to-audio.js @@ -0,0 +1,19 @@ +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +client.add(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + // Let's say the first file is an mp3 audio file + var file = torrent.files[0] + + // Create an audio element + var audio = document.createElement('audio') + audio.controls = true + document.body.appendChild(audio) + + // Stream the audio into the audio tag + file.createReadStream().pipe(audio) +}) diff --git a/examples/browser-stream-to-video.js b/examples/browser-stream-to-video.js new file mode 100644 index 0000000..7943a64 --- /dev/null +++ b/examples/browser-stream-to-video.js @@ -0,0 +1,19 @@ +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +client.add(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + // Let's say the first file is a webm (vp8) or mp4 (h264) video... + var file = torrent.files[0] + + // Create a video element + var video = document.createElement('video') + video.controls = true + document.body.appendChild(video) + + // Stream the video into the video tag + file.createReadStream().pipe(video) +}) diff --git a/examples/node-save-to-file-system.js b/examples/node-save-to-file-system.js new file mode 100644 index 0000000..5b33d0c --- /dev/null +++ b/examples/node-save-to-file-system.js @@ -0,0 +1,16 @@ +var WebTorrent = require('webtorrent') +var fs = require('fs') + +var client = new WebTorrent() + +client.download(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + torrent.files.forEach(function (file) { + // Stream each file to the disk + var source = file.createReadStream() + var destination = fs.createWriteStream(file.name) + source.pipe(destination) + }) +}) -- cgit v1.2.3