SpawnDev.BlazorJS.SimplePeer
1.0.1
See the version list below for details.
dotnet add package SpawnDev.BlazorJS.SimplePeer --version 1.0.1
NuGet\Install-Package SpawnDev.BlazorJS.SimplePeer -Version 1.0.1
<PackageReference Include="SpawnDev.BlazorJS.SimplePeer" Version="1.0.1" />
paket add SpawnDev.BlazorJS.SimplePeer --version 1.0.1
#r "nuget: SpawnDev.BlazorJS.SimplePeer, 1.0.1"
// Install SpawnDev.BlazorJS.SimplePeer as a Cake Addin #addin nuget:?package=SpawnDev.BlazorJS.SimplePeer&version=1.0.1 // Install SpawnDev.BlazorJS.SimplePeer as a Cake Tool #tool nuget:?package=SpawnDev.BlazorJS.SimplePeer&version=1.0.1
SpawnDev.BlazorJS.SimplePeer
SpawnDev.BlazorJS.SimplePeer brings the amazing SimplePeer library to Blazor WebAssembly.
SpawnDev.BlazorJS.SimplePeer uses SpawnDev.BlazorJS for Javascript interop allowing strongly typed, full usage of the SimplePeer Javascript library. Voice, video and data channels are all fully supported in Blazor WebAssembly. The SpawnDev.BlazorJS.SimplePeer API is a strongly typed version of the API found on the SimplePeer repo.
Demo
Getting started
Add the Nuget package SpawnDev.BlazorJS.SimplePeer
to your project using your package manager of choice.
Modify the Blazor WASM Program.cs
to initialize SpawnDev.BlazorJS for Javascript interop.
Example Program.cs
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using SpawnDev.BlazorJS;
using SpawnDev.BlazorJS.SimplePeer;
using SpawnDev.BlazorJS.SimplePeer.Demo;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
// Add SpawnDev.BlazorJS interop
builder.Services.AddBlazorJSRuntime();
// Load the SimplePeer Javascript library. Can be called in a component instead if desired.
await SimplePeer.Init();
// Run app using BlazorJSRunAsync extension
await builder.Build().BlazorJSRunAsync();
ManualConnectExample.razor
Modified version of this SimplePer usage example
@page "/ManualConnectExample"
@using SpawnDev.BlazorJS.JSObjects;
@using System.Text;
@using System.Text.Json;
@implements IDisposable
<PageTitle>Counter</PageTitle>
<h1>SimplePeer Test</h1>
<p>
This test lets you manually connect two peers by copying and pasting the signal messages. This example is meant to mirror the original. <a href="https://github.com/feross/simple-peer?tab=readme-ov-file#usage">Original Example</a>
</p>
<div>
Role: @PeerRole<br />
<button disabled="@(peer != null)" class="btn btn-primary" @onclick="@(()=>Init(true))">Create Initiator</button>
<button disabled="@(peer != null)" class="btn btn-primary" @onclick="@(()=>Init(false))">Create Receiver</button>
</div>
<div>
<textarea style="width: 600px; word-wrap: break-word; white-space: normal;" @bind=@incoming></textarea>
<button disabled="@(peer == null)" @onclick=@Submit>submit</button>
</div>
<pre style="width: 600px; word-wrap: break-word; white-space: normal;">@outgoing</pre>
@code {
[Inject] BlazorJSRuntime JS { get; set; }
string PeerRole => peer == null ? "(select)" : (peer.Initiator ? "initiator" : "receiver");
SimplePeer? peer = null;
Document? document = null;
string outgoing = "";
string incoming = "";
void Init(bool initiator)
{
peer = new SimplePeer(new SimplePeerOptions
{
Initiator = initiator,
Trickle = false,
}
);
peer.OnError += SimplePeer_OnError;
peer.OnSignal += SimplePeer_OnSignal;
peer.OnConnect += SimplePeer_OnConnect;
peer.OnClose += SimplePeer_OnClose;
peer.OnData += SimplePeer_OnData;
}
void Submit()
{
peer!.Signal(JSON.Parse(incoming)!);
}
void SimplePeer_OnConnect()
{
JS.Log("CONNECT");
peer!.Send("Hello " + Guid.NewGuid().ToString());
}
void SimplePeer_OnClose()
{
JS.Log("CLOSE");
}
void SimplePeer_OnSignal(JSObject data)
{
JS.Log("SIGNAL", JSON.Stringify(data));
outgoing = JSON.Stringify(data);
StateHasChanged();
}
void SimplePeer_OnError(NodeError error)
{
outgoing = error.Code!;
StateHasChanged();
}
void SimplePeer_OnData(NodeBuffer data)
{
outgoing = Encoding.UTF8.GetString((byte[])data!);
StateHasChanged();
}
public void Dispose()
{
if (peer != null)
{
peer.OnError -= SimplePeer_OnError;
peer.OnSignal -= SimplePeer_OnSignal;
peer.OnConnect -= SimplePeer_OnConnect;
peer.OnClose -= SimplePeer_OnClose;
peer.OnData -= SimplePeer_OnData;
peer.Destroy();
peer.Dispose();
peer = null;
}
if (document != null)
{
document.Dispose();
document = null;
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.AspNetCore.Components.WebAssembly (>= 6.0.25)
- SpawnDev.BlazorJS (>= 2.2.103)
-
net7.0
- Microsoft.AspNetCore.Components.WebAssembly (>= 7.0.14)
- SpawnDev.BlazorJS (>= 2.2.103)
-
net8.0
- Microsoft.AspNetCore.Components.WebAssembly (>= 8.0.1)
- SpawnDev.BlazorJS (>= 2.2.103)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.