Last active
April 29, 2016 19:43
-
-
Save ngnguyen1/9b2c197bea5c9926aad92c8b5fe07c10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var client = new WebTorrent(); | |
function reqListener () { | |
client.seed(this.response, {name: 'alan.mp4', urlList: ['http://localhost/alan.mp4']}, function (t) { | |
console.log('seeding .. ' + t.magnetURI); | |
t.files.forEach(function (f) { | |
// Append video to DOM | |
f.appendTo(document.querySelector('.vid'), function (err, elem) { | |
if (err) return console.log('Error' + err); | |
}) | |
}) | |
}) | |
} | |
var oReq = new XMLHttpRequest(); | |
oReq.addEventListener("load", reqListener); | |
oReq.responseType = "blob"; | |
oReq.open("GET", "http://localhost/alan.mp4"); | |
oReq.send(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<body> | |
<div class="vid"></div> | |
</body> | |
<script src="webtorrent.min.js"> | |
</script> | |
<script src="t.js"></script> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tổng hợp những kiến thức trong 2 ngày (28-29/4-2016) | |
Vấn đề: | |
Thư viện Webtorrent hiện tại chỉ support WebRTC protocol, (không support tcp/udp protocol do lý do bảo mật) điều đó có nghĩa là sẽ không thể lấy các pieces từ các client mà sử dụng giao thức tcp/udp, đồng thời có nghĩa rằng nếu trong torrent không có bất kì client nào sử dụng giao thức WebRTC thì sẽ người dùng sẽ không thể nào lấy được nội dung torrent. | |
Các giải quyết: | |
Đầu vào chúng ta là một magnetURI của một torrent mà không có client nào sử dụng giao thức WebRTC. | |
Ví dụ: | |
magnet:?xt=urn:btih:359c6570ffc6e5cd6c06e2a0bea75624ddbb7121&dn=alan.mp4&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.webtorrent.io&ws=http%3A%2F%2Flocalhost%2Falan.mp4 | |
Sử dụng javascript String.split() để lấy giá trị của tham số `ws=`. Đây là url của tệp tin mà chúng ta muốn share đã được encode. Rồi sử dụng decodeURI(encodeURL) method decode mà URL mà chúng ta nhận được. | |
=> Chúng ta sẽ có : `http://localhost/alan.mp4`. | |
Bởi vì thư viện WebTorrent không support url http argument nên chúng ta phải sử dụng một XHR request để lấy dữ liệu về rồi tiến hành seed nội dung đó. Video từ URL sẽ được appnend thẳng vào HTML đồng thời seeding với một magnetURI. | |
Như vậy vấn đề đã được giải quyết. | |
Tóm tắt kiến thức: | |
Chúng ta sẽ chia ra làm 2 trường hợp với một input: | |
- Nếu input là một torrent file (.torrent) chúng ta sẽ tiến hành quá trình torrenting - là quá trình mà client sẽ tiến hành receive các pieces từ các client khác (các client sử dụng giao thức WebRTC). Khi nhận được bất kì pieces nào, client sẽ tiến hành seeding luôn pieces đó. | |
- Nếu input là một file (không phải .torrent) hay đường dẫn một thư mục thì quá trình torrent bị bỏ qua và quá trình seeding sẽ được thực hiện ngay. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment