mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-05 22:33:08 +00:00
finalize rtc connection
This commit is contained in:
parent
edf885dd9c
commit
dec49341e1
@ -13,7 +13,7 @@ import QRCode from "qrcode";
|
|||||||
|
|
||||||
// Protobuf
|
// Protobuf
|
||||||
const ProtoMessage = new protobuf.Type("Message").add(
|
const ProtoMessage = new protobuf.Type("Message").add(
|
||||||
new protobuf.Field("data", 3, "bytes")
|
new protobuf.Field("data", 3, "string")
|
||||||
);
|
);
|
||||||
|
|
||||||
main();
|
main();
|
||||||
@ -90,12 +90,14 @@ async function main() {
|
|||||||
|
|
||||||
const { peerConnection, sendMessage: sendRTCMessage } = initRTC({
|
const { peerConnection, sendMessage: sendRTCMessage } = initRTC({
|
||||||
ui,
|
ui,
|
||||||
onReceive: ui.message.onReceive,
|
onReceive: ui.message.onReceive.bind(ui.message),
|
||||||
});
|
});
|
||||||
|
|
||||||
peerConnection.onicecandidate = async (event) => {
|
peerConnection.onicecandidate = async (event) => {
|
||||||
if (event.candidate) {
|
if (event.candidate) {
|
||||||
|
console.log("candidate sent");
|
||||||
try {
|
try {
|
||||||
|
// if (!peerConnection.remoteDescription) return;
|
||||||
ui.rtc.sendingCandidate();
|
ui.rtc.sendingCandidate();
|
||||||
await sendWakuMessage({
|
await sendWakuMessage({
|
||||||
type: "candidate",
|
type: "candidate",
|
||||||
@ -108,6 +110,7 @@ async function main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const sendOffer = async () => {
|
const sendOffer = async () => {
|
||||||
|
console.log("offer sent");
|
||||||
ui.rtc.sendingOffer();
|
ui.rtc.sendingOffer();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -124,7 +127,8 @@ async function main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const sendAnswer = async (data) => {
|
const sendAnswer = async (data) => {
|
||||||
ui.rtc.sendAnswer();
|
console.log("answer sent");
|
||||||
|
ui.rtc.sendingAnswer();
|
||||||
try {
|
try {
|
||||||
await peerConnection.setRemoteDescription(
|
await peerConnection.setRemoteDescription(
|
||||||
new RTCSessionDescription(data.offer)
|
new RTCSessionDescription(data.offer)
|
||||||
@ -143,18 +147,26 @@ async function main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const receiveAnswer = async (data) => {
|
const receiveAnswer = async (data) => {
|
||||||
await peerConnection.setRemoteDescription(
|
try {
|
||||||
new RTCSessionDescription(data.answer)
|
console.log("answer received");
|
||||||
);
|
await peerConnection.setRemoteDescription(
|
||||||
|
new RTCSessionDescription(data.answer)
|
||||||
|
);
|
||||||
|
console.log("answer saved");
|
||||||
|
|
||||||
await sendWakuMessage({
|
await sendWakuMessage({
|
||||||
type: "ready",
|
type: "ready",
|
||||||
text: "received answer",
|
text: "received answer",
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
ui.rtc.error(error.message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const receiveCandidate = async (data) => {
|
const receiveCandidate = async (data) => {
|
||||||
try {
|
try {
|
||||||
|
// if (!peerConnection.pendingRemoteDescription) return;
|
||||||
|
console.log("candidate saved");
|
||||||
await peerConnection.addIceCandidate(
|
await peerConnection.addIceCandidate(
|
||||||
new RTCIceCandidate(data.candidate)
|
new RTCIceCandidate(data.candidate)
|
||||||
);
|
);
|
||||||
@ -183,7 +195,12 @@ async function main() {
|
|||||||
|
|
||||||
await listenToWakuMessages(handleWakuMessages);
|
await listenToWakuMessages(handleWakuMessages);
|
||||||
ui.message.onSend(sendRTCMessage);
|
ui.message.onSend(sendRTCMessage);
|
||||||
ui.rtc.onConnect(sendOffer);
|
|
||||||
|
// if we are initiator of Noise handshake
|
||||||
|
// let's initiate Web RTC as well
|
||||||
|
if (!urlPairingInfo) {
|
||||||
|
await sendOffer();
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
ui.waku.error(err.message);
|
ui.waku.error(err.message);
|
||||||
ui.hide();
|
ui.hide();
|
||||||
@ -288,7 +305,8 @@ async function buildWakuMessage(node, noiseExecute) {
|
|||||||
|
|
||||||
const sendMessage = async (message) => {
|
const sendMessage = async (message) => {
|
||||||
let payload = ProtoMessage.create({
|
let payload = ProtoMessage.create({
|
||||||
data: utils.utf8ToBytes(JSON.stringify(message)),
|
// data: utils.utf8ToBytes(JSON.stringify(message)),
|
||||||
|
data: JSON.stringify(message),
|
||||||
});
|
});
|
||||||
payload = ProtoMessage.encode(payload).finish();
|
payload = ProtoMessage.encode(payload).finish();
|
||||||
|
|
||||||
@ -296,9 +314,10 @@ async function buildWakuMessage(node, noiseExecute) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const listenToMessages = async (fn) => {
|
const listenToMessages = async (fn) => {
|
||||||
return node.filter.subscribe([decoder], (payload) => {
|
return node.filter.subscribe([decoder], ({ payload }) => {
|
||||||
const { data } = ProtoMessage.decode(payload);
|
const { data } = ProtoMessage.decode(payload);
|
||||||
fn(JSON.parse(utils.bytesToUtf8(data)));
|
// fn(JSON.parse(utils.bytesToUtf8(data)));
|
||||||
|
fn(JSON.parse(data));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -306,7 +325,9 @@ async function buildWakuMessage(node, noiseExecute) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function initRTC({ ui, onReceive }) {
|
function initRTC({ ui, onReceive }) {
|
||||||
const configuration = {};
|
const configuration = {
|
||||||
|
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
||||||
|
};
|
||||||
const peerConnection = new RTCPeerConnection(configuration);
|
const peerConnection = new RTCPeerConnection(configuration);
|
||||||
const sendChannel = peerConnection.createDataChannel("chat");
|
const sendChannel = peerConnection.createDataChannel("chat");
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user