]> git.r.bdr.sh - rbdr/junction/blame_incremental - extension/content_script.js
Update for socket.io 4
[rbdr/junction] / extension / content_script.js
... / ...
CommitLineData
1import { io } from 'socket.io-client';
2import Peers from './peers';
3import Media from './media';
4
5const internals = {
6
7 kSocketUrl: 'https://junction.tranquil.services',
8 kIceServers: [
9 {url:"stun:stun.l.google.com:19302"}
10 ],
11
12 port: null,
13 socket: null,
14 peers: {},
15
16 onMessage(message) {
17 internals[message.action](message.data);
18 },
19
20 async joinAudioCall(data) {
21
22 internals.tada = data.tada; // Keeping for fun
23
24 try {
25 const mediaStream = await Media.start();
26
27 internals.socket = io(internals.kSocketUrl, {
28 transports: ['websocket']
29 });
30
31 internals.socket.on('error', function(error) {
32
33 console.error('GENERAL ERROR', error);
34 });
35
36 internals.socket.on('connect_error', function(error) {
37
38 console.error('CONNNECT ERROR', error);
39 });
40
41 internals.socket.on('connect', function() {
42
43 console.log("Connected to signaling server, group: ", data.currentUrl);
44 internals.socket.emit('join', {
45 'url': data.currentUrl,
46 });
47 });
48
49 internals.socket.on('disconnect', function() {
50
51 console.log("disconnected from signaling server");
52 });
53
54 internals.socket.on('addPeer', function(data) {
55
56 console.log(data);
57 Peers.add(data.peer_id, internals.tada);
58 const peerId = data.peer_id;
59
60 const peerConn = new RTCPeerConnection(
61 {"iceServers": internals.kIceServers},
62 {"optional": [{"DtlsSrtpKeyAgreement": true}]}
63 );
64
65 internals.peers[peerId] = peerConn;
66
67 peerConn.onicecandidate = (event) => {
68 if (event.candidate) {
69 internals.socket.emit('relayICECandidate', {
70 'peer_id': peerId,
71 'ice_candidate': {
72 'sdpMLineIndex': event.candidate.sdpMLineIndex,
73 'candidate': event.candidate.candidate
74 }
75 });
76 }
77 }
78
79 peerConn.onaddstream = (stream) => {
80 console.log(`Received stream for peer ${peerId}`);
81 console.log(stream);
82 }
83
84 peerConn.addStream(mediaStream);
85
86 if (data.should_create_offer) {
87 console.log("Creating RTC offer to ", peerId);
88 peerConn.createOffer((local_description) => {
89 console.log("Local offer description is: ", local_description);
90 peerConn.setLocalDescription(local_description, () => {
91 internals.socket.emit('relaySessionDescription', {
92 'peer_id': peerId,
93 'session_description': local_description
94 });
95
96 console.log("Offer setLocalDescription succeeded");
97 }, () => { console.log("Offer setLocalDescription failed!"); }
98 );
99 },
100 (error) => { console.log("Error sending offer: ", error) }
101 );
102 }
103 console.log(`There are now ${Peers.count()} participants`);
104 });
105
106 internals.socket.on('removePeer', function() {
107
108 Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids.
109 console.log(`There are now ${Peers.count()} participants`);
110 });
111 }
112 catch (err) {
113
114 internals.port.postMessage({
115 action: 'error'
116 });
117 internals.port.disconnect();
118 }
119 },
120
121 hangUp() {
122
123 Peers.reset();
124 Media.stop();
125 internals.socket.close();
126 internals.port.disconnect();
127 }
128};
129
130internals.port = chrome.runtime.connect({ name:"content" });
131internals.port.onMessage.addListener(internals.onMessage);
132
133console.log('Content Script Loaded');
134
135// Indicates to the background script that we executed correctly
136true;