-'use strict';
+import { io } from 'socket.io-client';
+import Peers from './peers';
+import Media from './media';
-(() => {
+const internals = {
- const io = require('socket.io-client');
- const Peers = require('./peers');
- const Media = require('./media');
+ kSocketUrl: 'ws://localhost:8000',
- const internals = {
+ port: null,
+ socket: null,
+ peers: 0,
- kSocketUrl: 'https://junction.unlimited.pizza/',
+ onMessage(message) {
+ internals[message.action](message.data);
+ },
- port: null,
- socket: null,
- peers: 0,
+ async joinAudioCall(data) {
- onMessage(message) {
- internals[message.action](message.data);
- },
+ internals.tada = data.tada; // Keeping for fun
- async joinAudioCall(data) {
+ try {
+ await Media.start();
- internals.tada = data.tada; // Keeping for fun
+ internals.socket = io(internals.kSocketUrl, {
+ transports: ['websocket']
+ });
- try {
- const mediaStream = await Media.start();
+ internals.socket.on('error', function(error) {
- internals.socket = io(internals.kSocketUrl, {
- transports: ['websocket']
- });
+ console.error('GENERAL ERROR', error);
+ });
- internals.socket.on('error', function(error) {
+ internals.socket.on('connect_error', function(error) {
- console.error('GENERAL ERROR', error);
- });
+ console.error('CONNNECT ERROR', error);
+ });
- internals.socket.on('connect_error', function(error) {
+ internals.socket.on('connect', function() {
- console.error('CONNNECT ERROR', error);
+ console.log("Connected to signaling server, group: ", data.currentUrl);
+ internals.socket.emit('join', {
+ 'url': data.currentUrl,
});
+ });
- internals.socket.on('connect', function() {
+ internals.socket.on('disconnect', function() {
- console.log("Connected to signaling server, group: ", data.currentUrl);
- internals.socket.emit('join', {
- 'url': data.currentUrl,
- });
- });
+ console.log("disconnected from signaling server");
+ });
- internals.socket.on('disconnect', function() {
+ internals.socket.on('addPeer', function(data) {
- console.log("disconnected from signaling server");
- });
+ console.log(data);
+ Peers.add('id-'+Peers.count(), internals.tada);
+ console.log(`There are now ${Peers.count()} participants`);
+ });
- internals.socket.on('addPeer', function(data) {
+ internals.socket.on('removePeer', function() {
- console.log(data);
- Peers.add('id-'+Peers.count(), internals.tada);
- console.log(`There are now ${Peers.count()} participants`);
- });
-
- internals.socket.on('removePeer', function() {
+ Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids.
+ console.log(`There are now ${Peers.count()} participants`);
+ });
+ }
+ catch (err) {
- Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids.
- console.log(`There are now ${Peers.count()} participants`);
- });
- }
- catch (err) {
+ internals.port.postMessage({
+ action: 'error'
+ });
+ internals.port.disconnect();
+ }
+ },
- internals.port.postMessage({
- action: 'error'
- });
- internals.port.disconnect();
- }
- },
+ hangUp() {
- hangUp() {
+ Peers.reset();
+ Media.stop();
+ internals.socket.close();
+ internals.port.disconnect();
+ }
+};
- Peers.reset();
- Media.stop();
- internals.socket.close();
- internals.port.disconnect();
- }
- };
+internals.port = chrome.runtime.connect({ name:"content" });
+internals.port.onMessage.addListener(internals.onMessage);
- internals.port = chrome.runtime.connect({ name:"content" });
- internals.port.onMessage.addListener(internals.onMessage);
-})();
+console.log('Content Script Loaded');
// Indicates to the background script that we executed correctly
true;