UNPKG

@devexcelsior/healing-webrtc-relay

Version:

41 lines (40 loc) 1.39 kB
"use strict"; // Formerly © 2025 Curt Puckett / DevExcelsior – all rights waived under the Unlicense. // This code is now public domain. No rights reserved. No attribution required. Object.defineProperty(exports, "__esModule", { value: true }); exports.HealingPeerRelay = void 0; class HealingPeerRelay { constructor() { this.connection = new RTCPeerConnection(); } async createOffer() { const offer = await this.connection.createOffer(); await this.connection.setLocalDescription(offer); return JSON.stringify(offer); } async receiveAnswer(answer) { const remoteDesc = new RTCSessionDescription(JSON.parse(answer)); await this.connection.setRemoteDescription(remoteDesc); } onSignal(callback) { this.connection.ondatachannel = (event) => { const channel = event.channel; channel.onmessage = (msg) => { try { const parsed = JSON.parse(msg.data); callback(parsed); } catch { // ignore } }; }; } sendSignal(data) { const channel = this.connection.createDataChannel('healing'); channel.onopen = () => { channel.send(JSON.stringify(data)); }; } } exports.HealingPeerRelay = HealingPeerRelay;