]>
Commit | Line | Data |
---|---|---|
4757c461 RBR |
1 | (() => { |
2 | ||
d4fb48eb RBR |
3 | const io = require('socket.io-client'); |
4 | ||
4757c461 | 5 | const internals = { |
d4fb48eb | 6 | |
02071d8e | 7 | kSocketUrl: 'https://junction.unlimited.pizza/', |
d4fb48eb | 8 | |
4757c461 | 9 | port: null, |
d4fb48eb RBR |
10 | socket: null, |
11 | peers: 0, | |
4757c461 RBR |
12 | |
13 | onMessage(message) { | |
14 | internals[message.action](message.data); | |
15 | }, | |
16 | ||
17 | async joinAudioCall(data) { | |
18 | ||
19 | try { | |
20 | const mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true }); | |
d4fb48eb | 21 | |
02071d8e RBR |
22 | internals.socket = io(internals.kSocketUrl, { |
23 | transports: ['websocket'] | |
24 | }); | |
25 | ||
26 | internals.socket.on('error', function(error) { | |
27 | ||
28 | console.error('GENERAL ERROR', error); | |
29 | }); | |
30 | ||
31 | internals.socket.on('connect_error', function(error) { | |
32 | ||
33 | console.error('CONNNECT ERROR', error); | |
34 | }); | |
d4fb48eb RBR |
35 | |
36 | internals.socket.on('connect', function() { | |
37 | ||
02071d8e | 38 | console.log("Connected to signaling server, group: ", data.currentUrl); |
d4fb48eb | 39 | internals.socket.emit('join', { |
02071d8e | 40 | 'url': data.currentUrl, |
d4fb48eb RBR |
41 | }); |
42 | }); | |
43 | ||
44 | internals.socket.on('disconnect', function() { | |
45 | ||
46 | console.log("disconnected from signaling server"); | |
47 | }); | |
48 | ||
49 | internals.socket.on('addPeer', function(data) { | |
50 | ||
51 | console.log(data); | |
52 | internals.peers++; | |
53 | console.log(`There are now ${internals.peers} participants`); | |
54 | }); | |
55 | ||
56 | internals.socket.on('removePeer', function() { | |
57 | ||
58 | internals.peers--; | |
59 | console.log(`There are now ${internals.peers} participants`); | |
60 | }); | |
61 | ||
4757c461 RBR |
62 | internals.createAudioElement(data.tada); |
63 | } | |
64 | catch (err) { | |
65 | ||
66 | internals.port.postMessage({ | |
67 | action: 'error' | |
68 | }); | |
69 | internals.port.disconnect(); | |
70 | internals.createAudioElement(data.tada); | |
71 | } | |
72 | }, | |
73 | ||
74 | hangUp() { | |
75 | document.querySelectorAll('.junction-call-audio').forEach((audioElement) => audioElement.remove()); | |
d4fb48eb | 76 | internals.socket.close(); |
4757c461 RBR |
77 | internals.port.disconnect(); |
78 | }, | |
79 | ||
80 | createAudioElement(source, type = 'audio/wav') { | |
81 | ||
82 | const audioElement = document.createElement('audio'); | |
83 | audioElement.setAttribute('class', 'junction-call-audio'); | |
84 | audioElement.src = source; | |
85 | audioElement.autoplay = 'autoplay'; | |
86 | audioElement.type = type; | |
87 | document.querySelector('body').appendChild(audioElement); | |
88 | } | |
89 | }; | |
90 | ||
91 | internals.port = chrome.runtime.connect({ name:"content" }); | |
92 | internals.port.onMessage.addListener(internals.onMessage); | |
93 | })(); | |
94 | ||
95 | // Indicates to the background script that we executed correctly | |
96 | true; |