- commit
- 96ae6600205d103c788dde204cd7827b5a1f1dbb
- parent
- f0ec99554b94c29b584a00420aec7543660b68cc
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-05-05 05:47
refactor
Diffstat
M | index.html | 2 | +- |
M | static/chat.js | 4 | ++-- |
M | static/rtc.js | 29 | ++++++++++++++++++----------- |
R | static/signal.js -> static/utils.js | 18 | ------------------ |
A | static/via.js | 16 | ++++++++++++++++ |
5 files changed, 37 insertions, 32 deletions
diff --git a/index.html b/index.html
@@ -2,7 +2,7 @@ 2 2 <html> 3 3 <head> 4 4 <meta charset="utf-8" />5 -1 <title>duct rtc</title>-1 5 <title>rtc</title> 6 6 <meta http-equiv="Content-Security-Policy" content="default-src 'self'; connect-src http://via.ce9e.org"> 7 7 <meta name="viewport" content="width=device-width" /> 8 8 <link rel="stylesheet" href="static/common.css">
diff --git a/static/chat.js b/static/chat.js
@@ -1,4 +1,4 @@1 -1 import * as signal from './signal.js';-1 1 import * as via from './via.js'; 2 2 3 3 var history = document.querySelector('.chat-history'); 4 4 var form = document.querySelector('.chat-form'); @@ -10,7 +10,7 @@ export var init = function(room, queue) { 10 10 if (!input.value) { 11 11 return; 12 12 }13 -1 signal.post(room, {sender: queue, type: 'chat', text: input.value}).then(function() {-1 13 via.send(room, {sender: queue, type: 'chat', text: input.value}).then(function() { 14 14 input.value = ''; 15 15 }); 16 16 });
diff --git a/static/rtc.js b/static/rtc.js
@@ -1,6 +1,7 @@ 1 1 /* eslint no-console: "off" */ 2 23 -1 import * as signal from './signal.js';-1 3 import * as via from './via.js'; -1 4 import * as utils from './utils.js'; 4 5 import * as chat from './chat.js'; 5 6 import * as media from './media.js'; 6 7 @@ -10,14 +11,20 @@ import * as media from './media.js'; 10 11 // ICE -- networking information 11 12 // offer/answer -- media stream information 12 1313 -1 var room = location.hash.substr(1);14 -1 var queue = signal.randomString(10);15 -1 var queuePassword = signal.randomString(10);-1 14 if (!location.hash) { -1 15 location.hash = utils.randomString(10); -1 16 } -1 17 -1 18 var room = 'rtc/' + location.hash.substr(1); -1 19 var queue = 'rtcq/' + utils.randomString(10); -1 20 var queuePassword = utils.randomString(10); 16 21 console.log('own queue', queue); 17 22 18 23 var container = document.querySelector('.rtc-videos'); 19 24 var cons = {}; 20 25 -1 26 document.title += ' - ' + location.hash.slice(1); -1 27 21 28 var closeConnection = function(sender) { 22 29 if (sender in cons) { 23 30 cons[sender].video.remove(); @@ -39,7 +46,7 @@ var getConnection = function(sender) { 39 46 iceServers: [{urls: 'stun:ce9e.org:3478'}], 40 47 }); 41 48 con.addEventListener('icecandidate', function(event) {42 -1 signal.post(sender, {sender: queue, type: 'candidate', data: event.candidate});-1 49 via.send(sender, {sender: queue, type: 'candidate', data: event.candidate}); 43 50 }); 44 51 con.addEventListener('negotiationneeded', function() { 45 52 makeOffer(sender); @@ -69,7 +76,7 @@ var makeOffer = function(sender) { 69 76 var con = getConnection(sender); 70 77 con.createOffer().then(offer => { 71 78 con.setLocalDescription(offer).then(() => {72 -1 signal.post(sender, {sender: queue, type: 'offer', data: offer});-1 79 via.send(sender, {sender: queue, type: 'offer', data: offer}); 73 80 }); 74 81 }); 75 82 } @@ -80,7 +87,7 @@ var handleOffer = function(sender, offer) { 80 87 con.setRemoteDescription(offer).then(() => { 81 88 con.createAnswer().then(answer => { 82 89 con.setLocalDescription(answer).then(() => {83 -1 signal.post(sender, {sender: queue, type: 'answer', data: answer});-1 90 via.send(sender, {sender: queue, type: 'answer', data: answer}); 84 91 }); 85 92 }); 86 93 }); @@ -100,7 +107,7 @@ var handleCandidate = function(sender, candidate) { 100 107 } 101 108 }; 102 109103 -1 signal.listen(queue + ':' + queuePassword, function(msg) {-1 110 via.listen(queue + ':' + queuePassword, function(msg) { 104 111 if (msg.type === 'offer') { 105 112 handleOffer(msg.sender, msg.data); 106 113 } else if (msg.type === 'answer') { @@ -112,7 +119,7 @@ signal.listen(queue + ':' + queuePassword, function(msg) { 112 119 } 113 120 }); 114 121115 -1 signal.listen(room, function(msg) {-1 122 via.listen(room, function(msg) { 116 123 if (msg.type === 'chat') { 117 124 chat.addMsg(msg.sender, msg.text); 118 125 } else if (msg.type === 'announce') { @@ -125,7 +132,7 @@ signal.listen(room, function(msg) { 125 132 }); 126 133 127 134 window.addEventListener('beforeunload', function() {128 -1 signal.beacon(room, {sender: queue, type: 'leave'});-1 135 via.send(room, {sender: queue, type: 'leave'}, true); 129 136 }); 130 137 131 138 chat.init(room, queue); @@ -141,4 +148,4 @@ media.init(function() { 141 148 } 142 149 }); 143 150144 -1 signal.post(room, {sender: queue, type: 'announce'});-1 151 via.send(room, {sender: queue, type: 'announce'});
diff --git a/static/signal.js b/static/utils.js
@@ -1,5 +1,4 @@ 1 1 var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';2 -1 var baseUrl = 'https://via.ce9e.org/';3 2 4 3 export var randomString = function(length) { 5 4 var result = []; @@ -9,20 +8,3 @@ export var randomString = function(length) { 9 8 } 10 9 return result.join(''); 11 10 };12 -113 -1 export var post = function(key, data) {14 -1 return fetch(baseUrl + key, {method: 'POST', body: JSON.stringify(data)});15 -1 };16 -117 -1 export var beacon = function(key, data) {18 -1 return navigator.sendBeacon(baseUrl + key, JSON.stringify(data));19 -1 };20 -121 -1 export var listen = function(key, fn) {22 -1 var evtSource = new EventSource(baseUrl + key + '?sse');23 -1 evtSource.onmessage = msg => fn(JSON.parse(msg.data));24 -1 };25 -126 -1 if (!location.hash) {27 -1 location.hash = randomString(10);28 -1 }
diff --git a/static/via.js b/static/via.js
@@ -0,0 +1,16 @@ -1 1 var baseUrl = 'https://via.ce9e.org/'; -1 2 -1 3 export var send = function(key, data, beacon) { -1 4 var body = JSON.stringify(data); -1 5 var url = baseUrl + 'msg/' + key; -1 6 if (beacon) { -1 7 return navigator.sendBeacon(url, body); -1 8 } else { -1 9 return fetch(url, {method: 'POST', body: body}); -1 10 } -1 11 }; -1 12 -1 13 export var listen = function(key, fn) { -1 14 var evtSource = new EventSource(baseUrl + 'msg/' + key); -1 15 evtSource.onmessage = msg => fn(JSON.parse(msg.data)); -1 16 };