]>
Commit | Line | Data |
---|---|---|
1 | import { io } from 'socket.io-client'; | |
2 | import Peers from './peers'; | |
3 | import Media from './media'; | |
4 | ||
5 | const internals = { | |
6 | ||
7 | kSocketUrl: 'ws://localhost:8000', | |
8 | ||
9 | port: null, | |
10 | socket: null, | |
11 | peers: 0, | |
12 | ||
13 | onMessage(message) { | |
14 | internals[message.action](message.data); | |
15 | }, | |
16 | ||
17 | async joinAudioCall(data) { | |
18 | ||
19 | internals.tada = data.tada; // Keeping for fun | |
20 | ||
21 | try { | |
22 | await Media.start(); | |
23 | ||
24 | internals.socket = io(internals.kSocketUrl, { | |
25 | transports: ['websocket'] | |
26 | }); | |
27 | ||
28 | internals.socket.on('error', function(error) { | |
29 | ||
30 | console.error('GENERAL ERROR', error); | |
31 | }); | |
32 | ||
33 | internals.socket.on('connect_error', function(error) { | |
34 | ||
35 | console.error('CONNNECT ERROR', error); | |
36 | }); | |
37 | ||
38 | internals.socket.on('connect', function() { | |
39 | ||
40 | console.log("Connected to signaling server, group: ", data.currentUrl); | |
41 | internals.socket.emit('join', { | |
42 | 'url': data.currentUrl, | |
43 | }); | |
44 | }); | |
45 | ||
46 | internals.socket.on('disconnect', function() { | |
47 | ||
48 | console.log("disconnected from signaling server"); | |
49 | }); | |
50 | ||
51 | internals.socket.on('addPeer', function(data) { | |
52 | ||
53 | console.log(data); | |
54 | Peers.add('id-'+Peers.count(), internals.tada); | |
55 | console.log(`There are now ${Peers.count()} participants`); | |
56 | }); | |
57 | ||
58 | internals.socket.on('removePeer', function() { | |
59 | ||
60 | Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids. | |
61 | console.log(`There are now ${Peers.count()} participants`); | |
62 | }); | |
63 | } | |
64 | catch (err) { | |
65 | ||
66 | internals.port.postMessage({ | |
67 | action: 'error' | |
68 | }); | |
69 | internals.port.disconnect(); | |
70 | } | |
71 | }, | |
72 | ||
73 | hangUp() { | |
74 | ||
75 | Peers.reset(); | |
76 | Media.stop(); | |
77 | internals.socket.close(); | |
78 | internals.port.disconnect(); | |
79 | } | |
80 | }; | |
81 | ||
82 | internals.port = chrome.runtime.connect({ name:"content" }); | |
83 | internals.port.onMessage.addListener(internals.onMessage); | |
84 | ||
85 | console.log('Content Script Loaded'); | |
86 | ||
87 | // Indicates to the background script that we executed correctly | |
88 | true; |