How to use webtorrent.io

房东的猫 提交于 2020-01-16 18:19:09

问题


https://webtorrent.io

https://github.com/feross/webtorrent

JavaScript to stream the following torrent in the browser

magnet:?xt=urn:btih:6A9759BFFD5C0AF65319979FB7832189F4F3C35D&dn=sintel.mp4&tr=udp%3a%2f%2ftracker.openbittorrent.com%3a80&tr=udp%3a%2f%2ftracker.internetwarriors.net%3a1337&tr=udp%3a%2f%2ftracker.leechers-paradise.org%3a6969&tr=udp%3a%2f%2ftracker.coppersurfer.tk%3a6969&tr=udp%3a%2f%2fexodus.desync.com%3a6969&tr=wss%3a%2f%2ftracker.webtorrent.io&tr=wss%3a%2f%2ftracker.btorrent.xyz&ws=https%3a%2f%2fwebtorrent.io%2ftorrents%2fsintel-1024-surround.mp4

I want a simple layout like

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js"></script>
</head>
<body>
<script type="text/javascript">

var client = new WebTorrent()

var torrentId = 'magnet:?xt=urn:btih:6A9759BFFD5C0AF65319979FB7832189F4F3C35D'

client.add(torrentId, function (torrent) {
  var file = torrent.files[0]
  file.appendTo('body') // append the file to the DOM
})
</script>
</body>
</html>

But this doesn't work.


回答1:


The code you posted doesn't work in web browser, because WebTorrent doesn't support trackerless magnet URIs in web browser environment (at least at this moment).

Just add at least one WebRTC tracker to your magnet URI, e.g. wss://tracker.webtorrent.io:

var torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&tr=wss%3A%2F%2Ftracker.webtorrent.io'

or use HTTP(S) URL to *.torrent file:

var torrentId = 'https://webtorrent.io/torrents/sintel.torrent'


来源:https://stackoverflow.com/questions/34082637/how-to-use-webtorrent-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!