rtc

minimal webrtc client
git clone git://ce9e.org/rtc

commit
f65d18e27c2eb54a196684337667bee90255b1fc
parent
910d4004675eb01a222abefb6720dde009d6a7ae
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-04-01 05:52
signal leave

Diffstat

M www/patch.js 5 +++++
M www/rtc/rtc.js 15 +++++++++++++--

2 files changed, 18 insertions, 2 deletions


diff --git a/www/patch.js b/www/patch.js

@@ -19,6 +19,10 @@
   19    19 		return fetch(baseUrl + key, {method: 'POST', body: JSON.stringify(data)});
   20    20 	};
   21    21 
   -1    22 	var beacon = function(key, data) {
   -1    23 		return navigator.sendBeacon(baseUrl + key, JSON.stringify(data));
   -1    24 	};
   -1    25 
   22    26 	var listen = function(key, fn) {
   23    27 		var evtSource = new EventSource(baseUrl + key + '?sse');
   24    28 		evtSource.onmessage = msg => fn(JSON.parse(msg.data));
@@ -26,6 +30,7 @@
   26    30 
   27    31 	window.patch = {
   28    32 		'post': post,
   -1    33 		'beacon': beacon,
   29    34 		'listen': listen,
   30    35 		'randomString': randomString,
   31    36 	};

diff --git a/www/rtc/rtc.js b/www/rtc/rtc.js

@@ -28,6 +28,12 @@
   28    28 		container.style.setProperty('--video-width', width + 'px');
   29    29 	};
   30    30 
   -1    31 	var closeConnection = function(sender) {
   -1    32 		cons[sender].video.remove();
   -1    33 		cons[sender].con.close();
   -1    34 		delete cons[sender];
   -1    35 	};
   -1    36 
   31    37 	var getConnection = function(sender) {
   32    38 		if (sender in cons) {
   33    39 			return cons[sender].con;
@@ -49,8 +55,7 @@
   49    55 		});
   50    56 		con.addEventListener('iceconnectionstatechange', function() {
   51    57 			if (con.iceConnectionState === 'disconnected') {
   52    -1 				video.remove();
   53    -1 				delete cons[sender];
   -1    58 				closeConnection(sender);
   54    59 			}
   55    60 		});
   56    61 		con.addEventListener('track', function(event) {
@@ -122,6 +127,10 @@
  122   127 
  123   128 	patch.post(room, {type: 'announce', key: queue});
  124   129 
   -1   130 	window.addEventListener('beforeunload', function() {
   -1   131 		patch.beacon(room, {type: 'leave', key: queue});
   -1   132 	});
   -1   133 
  125   134 	var updateConnections = function() {
  126   135 		var sender;
  127   136 		var tracks = localVideo.srcObject.getTracks();
@@ -227,6 +236,8 @@
  227   236 			addChatMsg(data.msg);
  228   237 		} else if (data.type === 'announce') {
  229   238 			makeOffer(data.key);
   -1   239 		} else if (data.type === 'leave') {
   -1   240 			closeConnection(data.key);
  230   241 		} else {
  231   242 			console.log('unknown message', data);
  232   243 		}