SpawnDev.BlazorJS.SimplePeer 1.0.1

There is a newer version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SpawnDev.BlazorJS.SimplePeer" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SpawnDev.BlazorJS.SimplePeer --version 1.0.1                
#r "nuget: SpawnDev.BlazorJS.SimplePeer, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

NuGet version

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

Simple 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 92 7/26/2024
1.2.1 93 7/23/2024
1.2.0 79 7/17/2024
1.1.1 113 7/4/2024
1.1.0 115 6/18/2024
1.0.3 112 6/5/2024
1.0.2 110 5/30/2024
1.0.1 110 5/29/2024
1.0.0 110 5/28/2024
1.0.0-rc1 112 5/28/2024