SpawnDev.BlazorJS.WebTorrents brings the amazing WebTorrent library to Blazor WebAssembly.
WebTorrent is a streaming torrent client for node.js and the browser. YEP, THAT'S RIGHT. THE BROWSER. It's written completely in JavaScript – the language of the web – so the same code works in both runtimes.
SpawnDev.BlazorJS.WebTorrents is a collection of JSObject wrappers that allow access to the Javascript WebTorrent library. The interfaces are nearly identical. Intellisense documentation is included.
Example Program.cs
usingMicrosoft.AspNetCore.Components.Web;usingMicrosoft.AspNetCore.Components.WebAssembly.Hosting;usingSpawnDev.BlazorJS;usingSpawnDev.BlazorJS.WebTorrents;usingSpawnDev.BlazorJS.WebTorrents.Demo;varbuilder=WebAssemblyHostBuilder.CreateDefault(args);builder.RootComponents.Add<App>("#app");builder.RootComponents.Add<HeadOutlet>("head::after");// Add BlazorJSRunAsync servicebuilder.Services.AddBlazorJSRuntime();// Add WebTorrentService servicebuilder.Services.AddWebTorrentService();// initialize BlazorJSRuntime to start appawaitbuilder.Build().BlazorJSRunAsync();Inject
[Inject]WebTorrentService WebTorrentService {get;set;}WebTorrentService.Client is an instance of WebTorrent
Very basic example that adds a torrent magnet, waits for the torrent metadata to be retrieved and shows information about the torrents files. Then, the torrent and all related data is destroyed.
varbigBuckBunnyMagnet="magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent";// add a torrent (only looking to get metadata here, so add it with the Deselect option set to true)usingvartorrent=WebTorrentService.Client!.Add(bigBuckBunnyMagnet,newAddTorrentOptions{Deselect=true});// wait for the torrent metadata to loadawaittorrent.WhenReady();// show some torrent infoConsole.WriteLine($"InfoHash: {torrent.InfoHash}");Console.WriteLine($"Files: {torrent.Files.Length}");foreach(Filefileintorrent.Files){Console.WriteLine($"File: {file.Name}{file.Size}");}// destory the torrent and related resourcesawaittorrent.DestroyAsync(newDestroyTorrentOptions{DestroyStore=true});




