From: Ruben Beltran del Rio Date: Mon, 28 Aug 2023 20:09:55 +0000 (+0200) Subject: Update for socket.io 4 X-Git-Url: https://git.r.bdr.sh/rbdr/junction/commitdiff_plain/139f43c6caf963d66704fe1675bcc70735e21926?hp=90de3cbb942653f472bc663fc11749117b1f8873 Update for socket.io 4 --- diff --git a/Makefile b/Makefile index d65039b..d4c4f4d 100644 --- a/Makefile +++ b/Makefile @@ -7,3 +7,6 @@ start: docker-build: docker-compose build + +package: + cd extension && zip -r ../junction.xpi manifest.json junction.js build/content_script.js icons/ sounds/ diff --git a/extension/content_script.js b/extension/content_script.js index 279c52d..6785a65 100644 --- a/extension/content_script.js +++ b/extension/content_script.js @@ -4,11 +4,14 @@ import Media from './media'; const internals = { - kSocketUrl: 'ws://localhost:8000', + kSocketUrl: 'https://junction.tranquil.services', + kIceServers: [ + {url:"stun:stun.l.google.com:19302"} + ], port: null, socket: null, - peers: 0, + peers: {}, onMessage(message) { internals[message.action](message.data); @@ -19,7 +22,7 @@ const internals = { internals.tada = data.tada; // Keeping for fun try { - await Media.start(); + const mediaStream = await Media.start(); internals.socket = io(internals.kSocketUrl, { transports: ['websocket'] @@ -51,7 +54,52 @@ const internals = { internals.socket.on('addPeer', function(data) { console.log(data); - Peers.add('id-'+Peers.count(), internals.tada); + Peers.add(data.peer_id, internals.tada); + const peerId = data.peer_id; + + const peerConn = new RTCPeerConnection( + {"iceServers": internals.kIceServers}, + {"optional": [{"DtlsSrtpKeyAgreement": true}]} + ); + + internals.peers[peerId] = peerConn; + + peerConn.onicecandidate = (event) => { + if (event.candidate) { + internals.socket.emit('relayICECandidate', { + 'peer_id': peerId, + 'ice_candidate': { + 'sdpMLineIndex': event.candidate.sdpMLineIndex, + 'candidate': event.candidate.candidate + } + }); + } + } + + peerConn.onaddstream = (stream) => { + console.log(`Received stream for peer ${peerId}`); + console.log(stream); + } + + peerConn.addStream(mediaStream); + + if (data.should_create_offer) { + console.log("Creating RTC offer to ", peerId); + peerConn.createOffer((local_description) => { + console.log("Local offer description is: ", local_description); + peerConn.setLocalDescription(local_description, () => { + internals.socket.emit('relaySessionDescription', { + 'peer_id': peerId, + 'session_description': local_description + }); + + console.log("Offer setLocalDescription succeeded"); + }, () => { console.log("Offer setLocalDescription failed!"); } + ); + }, + (error) => { console.log("Error sending offer: ", error) } + ); + } console.log(`There are now ${Peers.count()} participants`); }); diff --git a/server/index.js b/server/index.js index 56a1df4..37104e1 100644 --- a/server/index.js +++ b/server/index.js @@ -15,7 +15,7 @@ server.on('connection', (socket) => { socket.on('join', (data) => { socket.join(data.url); - const sockets = server.sockets.adapter.rooms.get(data.url); + const sockets = server.of(data.url).sockets.keys(); sockets.forEach((peerId) => { if (peerId !== me) { const peer = server.sockets.sockets.get(peerId) @@ -34,7 +34,7 @@ server.on('connection', (socket) => { }); socket.on('relayICECandidate', (data) => { - const sockets = server.sockets.adapter.rooms.get(data.url); + const sockets = server.of(data.url).sockets.keys(); sockets.forEach((peerId) => { const peer = server.sockets.sockets.get(peerId) peer.emit(events.types.ICE_CANDIDATE_RECEIVED, events.ICECandidateReceived(me, data.ice_candidate)) @@ -44,7 +44,7 @@ server.on('connection', (socket) => { }); socket.on('relaySessionDescription', (data) => { - const sockets = server.sockets.adapter.rooms.get(data.url); + const sockets = server.of(data.url).sockets.keys(); sockets.forEach((peerId) => { const peer = server.sockets.sockets.get(peerId) peer.emit(events.types.SESSION_DESCRIPTION_RECEIVED, events.SessionDescriptionReceived(me, data.session_description))