]>
Commit | Line | Data |
---|---|---|
1 | const io = require('socket.io-client'); | |
2 | ||
3 | const internals = { | |
4 | ||
5 | promisesSupported: !!(window.browser), | |
6 | isInCallState: false, | |
7 | ||
8 | icons: { | |
9 | call: { | |
10 | 16: 'icons/action-16.png', | |
11 | 32: 'icons/action-32.png' | |
12 | }, | |
13 | ||
14 | hangUp: { | |
15 | 16: 'icons/hang_up-16.png', | |
16 | 32: 'icons/hang_up-32.png' | |
17 | } | |
18 | }, | |
19 | ||
20 | socket: null, | |
21 | ||
22 | peers: 0, | |
23 | ||
24 | onClick() { | |
25 | if (internals.isInCall()) { | |
26 | return internals.hangUp(); | |
27 | } | |
28 | ||
29 | return internals.joinAudioCall(); | |
30 | }, | |
31 | ||
32 | async joinAudioCall() { | |
33 | ||
34 | internals.isInCallState = true; | |
35 | internals.setIcon('hangUp'); | |
36 | const activeTabs = await internals.getActiveTabs(); | |
37 | ||
38 | const socketUrl = 'http://unlimited.pizza:8000/'; | |
39 | const currentUrl = activeTabs[0].url; | |
40 | ||
41 | this.socket = io(socketUrl); | |
42 | ||
43 | var that = this; | |
44 | ||
45 | this.socket.on('connect', function() { | |
46 | console.log("Connected to signaling server"); | |
47 | ||
48 | that.socket.emit('join', { | |
49 | 'url': currentUrl, | |
50 | }); | |
51 | }); | |
52 | ||
53 | this.socket.on('disconnect', function() { | |
54 | console.log("disconnected from signaling server"); | |
55 | }); | |
56 | ||
57 | this.socket.on('addPeer', function(data) { | |
58 | console.log(data); | |
59 | that.peers++; | |
60 | console.log(`There are now ${that.peers} participants`); | |
61 | }); | |
62 | ||
63 | this.socket.on('removePeer', function() { | |
64 | that.peers--; | |
65 | console.log(`There are now ${that.peers} participants`); | |
66 | }); | |
67 | ||
68 | console.log(activeTabs[0].url); // placeholder while we connect backend. | |
69 | internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav')); | |
70 | }, | |
71 | ||
72 | hangUp() { | |
73 | this.socket.close(); | |
74 | ||
75 | ||
76 | ||
77 | document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove()); | |
78 | internals.setIcon('call'); | |
79 | internals.isInCallState = false; | |
80 | }, | |
81 | ||
82 | createAudioElement(source, type = 'audio/wav') { | |
83 | ||
84 | const audioElement = document.createElement('audio'); | |
85 | audioElement.src = source; | |
86 | audioElement.autoplay = 'autoplay'; | |
87 | audioElement.type = type; | |
88 | document.querySelector('body').appendChild(audioElement); | |
89 | }, | |
90 | ||
91 | isInCall() { | |
92 | return internals.isInCallState; // this should be replaced with actually checking the built stuff | |
93 | }, | |
94 | ||
95 | setIcon(iconSet) { | |
96 | ||
97 | internals.getRoot().browserAction.setIcon({ | |
98 | path: internals.icons[iconSet] | |
99 | }); | |
100 | }, | |
101 | ||
102 | getRoot() { | |
103 | ||
104 | return window.browser || window.chrome; | |
105 | }, | |
106 | ||
107 | // Chrome doesn't yet implement the promise based tabs.query :'( | |
108 | ||
109 | getActiveTabs() { | |
110 | ||
111 | const query = { | |
112 | currentWindow: true, | |
113 | active: true | |
114 | }; | |
115 | ||
116 | if (internals.promisesSupported) { | |
117 | return internals.getRoot().tabs.query(query); | |
118 | } | |
119 | ||
120 | return new Promise((resolve, reject) => { | |
121 | ||
122 | internals.getRoot().tabs.query(query, (tabs) => { | |
123 | ||
124 | return resolve(tabs); | |
125 | }); | |
126 | }); | |
127 | }, | |
128 | }; | |
129 | ||
130 | internals.getRoot().browserAction.onClicked.addListener(internals.onClick); |