+ const remoteDescription = new RTCSessionDescription(offer);
+ await peerConnection.setRemoteDescription(remoteDescription);
+
+ const answer = await peerConnection.createAnswer();
+ await peerConnection.setLocalDescription(answer);
+
+ return { peerId, answer };
+}
+
+export async function processPeerAnswer({ peerId, answer }) {
+ console.info(`Processing answer for peer ${peerId}`);
+ const peerConnection = internals.peers[peerId];
+ const remoteDescription = new RTCSessionDescription(answer);
+ await peerConnection.setRemoteDescription(remoteDescription);
+}
+
+export async function addIceCandidate({ peerId, candidate }) {
+ console.info(`Adding ICE candidate for peer ${peerId}`);
+ const peerConnection = internals.peers[peerId];
+ console.info(peerConnection.signalingState);
+ const iceCandidate = new RTCIceCandidate(candidate);
+ await peerConnection.addIceCandidate(iceCandidate);
+}
+
+export function countPeers() {
+ return Object.keys(internals.peers).length;
+}
+
+export function resetPeers() {
+ for (const connection of Object.values(internals.peers)) {
+ connection.close();