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