|
|
|
@ -7,6 +7,7 @@ import {
|
|
|
|
|
} from "../common/types.ts";
|
|
|
|
|
|
|
|
|
|
import { pearServerSocketAddress, rtcConfiguration } from "../ui.config.ts";
|
|
|
|
|
import { WalkyError } from "./error.ts";
|
|
|
|
|
|
|
|
|
|
export type AcceptDecision = true | false;
|
|
|
|
|
|
|
|
|
@ -42,7 +43,7 @@ export const initNetworking = async (p: {
|
|
|
|
|
console.debug(data);
|
|
|
|
|
|
|
|
|
|
if ("error" in data) {
|
|
|
|
|
throw new Error(data.error);
|
|
|
|
|
throw new WalkyError(data.error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handler = on[data.kind];
|
|
|
|
@ -86,14 +87,8 @@ export const initNetworking = async (p: {
|
|
|
|
|
pa(msg.answer);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const rtcPeerConnection = (peerID: PeerID) => {
|
|
|
|
|
const rtcPeerConnection = () => {
|
|
|
|
|
const rtc = new RTCPeerConnection(rtcConfiguration);
|
|
|
|
|
rtc.addEventListener("icecandidate", (ev) => {
|
|
|
|
|
if (ev.candidate) {
|
|
|
|
|
send({ kind: "iceCandidate", candidate: ev.candidate, peerID });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const connected = new Promise((r) => {
|
|
|
|
|
rtc.addEventListener("iceconnectionstatechange", (_ev) => {
|
|
|
|
|
console.debug(rtc.iceConnectionState);
|
|
|
|
@ -114,7 +109,7 @@ export const initNetworking = async (p: {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { rtc, connected } = rtcPeerConnection(msg.peerID);
|
|
|
|
|
const { rtc, connected } = rtcPeerConnection();
|
|
|
|
|
connections.set(msg.peerID, rtc);
|
|
|
|
|
|
|
|
|
|
const dataChannelP: Promise<RTCDataChannel> = new Promise((r) => {
|
|
|
|
@ -130,6 +125,15 @@ export const initNetworking = async (p: {
|
|
|
|
|
await rtc.setLocalDescription(answer);
|
|
|
|
|
|
|
|
|
|
send({ kind: "connectAnswer", answer, peerID: msg.peerID });
|
|
|
|
|
rtc.addEventListener("icecandidate", (ev) => {
|
|
|
|
|
if (ev.candidate) {
|
|
|
|
|
send({
|
|
|
|
|
kind: "iceCandidate",
|
|
|
|
|
candidate: ev.candidate,
|
|
|
|
|
peerID: msg.peerID,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const connectedP = (async () => {
|
|
|
|
|
const dataChannel = await dataChannelP;
|
|
|
|
@ -142,7 +146,7 @@ export const initNetworking = async (p: {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const connectToPeer = async (peerID: PeerID) => {
|
|
|
|
|
const { rtc, connected } = rtcPeerConnection(peerID);
|
|
|
|
|
const { rtc, connected } = rtcPeerConnection();
|
|
|
|
|
connections.set(peerID, rtc);
|
|
|
|
|
|
|
|
|
|
const dataChannel = rtc.createDataChannel("data");
|
|
|
|
@ -151,6 +155,11 @@ export const initNetworking = async (p: {
|
|
|
|
|
await rtc.setLocalDescription(offer);
|
|
|
|
|
|
|
|
|
|
const answer = await sendConnectOffer(peerID, offer);
|
|
|
|
|
rtc.addEventListener("icecandidate", (ev) => {
|
|
|
|
|
if (ev.candidate) {
|
|
|
|
|
send({ kind: "iceCandidate", candidate: ev.candidate, peerID });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
await rtc.setRemoteDescription(answer);
|
|
|
|
|
|
|
|
|
|
const connectedP = (async () => {
|
|
|
|
|