rtc

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

commit
c48980906d5454a7f76f0253310cd08892e357f4
parent
0deef8cd1bc8c9756dab3cb3d2d4c9bf662303e6
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-04-03 15:50
refactor signalling messages

Diffstat

M www/rtc/rtc.js 49 +++++++++++++++++++++++--------------------------

1 files changed, 23 insertions, 26 deletions


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

@@ -50,7 +50,7 @@
   50    50 			iceServers: [{urls: 'stun:ce9e.org:3478'}],
   51    51 		});
   52    52 		con.addEventListener('icecandidate', function(event) {
   53    -1 			patch.post(sender, {'sender': queue, 'data': event.candidate});
   -1    53 			patch.post(sender, {sender: queue, type: 'candidate', data: event.candidate});
   54    54 		});
   55    55 		con.addEventListener('negotiationneeded', function() {
   56    56 			makeOffer(sender);
@@ -85,7 +85,7 @@
   85    85 			var con = getConnection(sender);
   86    86 			con.createOffer().then(offer => {
   87    87 				con.setLocalDescription(offer).then(() => {
   88    -1 					patch.post(sender, {'sender': queue, 'data': offer});
   -1    88 					patch.post(sender, {sender: queue, type: 'offer', data: offer});
   89    89 				});
   90    90 			});
   91    91 		}
@@ -96,7 +96,7 @@
   96    96 		con.setRemoteDescription(offer).then(() => {
   97    97 			con.createAnswer().then(answer => {
   98    98 				con.setLocalDescription(answer).then(() => {
   99    -1 					patch.post(sender, {'sender': queue, 'data': answer});
   -1    99 					patch.post(sender, {sender: queue, type: 'answer', data: answer});
  100   100 				});
  101   101 			});
  102   102 		});
@@ -116,25 +116,22 @@
  116   116 		}
  117   117 	};
  118   118 
  119    -1 	patch.listen(queue + ':' + queuePassword, function(event) {
  120    -1 		var sender = event.sender;
  121    -1 		var data = event.data;
  122    -1 		if (sender && data) {
  123    -1 			if (data.type === 'offer') {
  124    -1 				return handleOffer(sender, data);
  125    -1 			} else if (data.type === 'answer') {
  126    -1 				return handleAnswer(sender, data);
  127    -1 			} else if (data.candidate) {
  128    -1 				return handleCandidate(sender, data);
  129    -1 			}
   -1   119 	patch.listen(queue + ':' + queuePassword, function(msg) {
   -1   120 		if (msg.type === 'offer') {
   -1   121 			handleOffer(msg.sender, msg.data);
   -1   122 		} else if (msg.type === 'answer') {
   -1   123 			handleAnswer(msg.sender, msg.data);
   -1   124 		} else if (msg.type === 'candidate') {
   -1   125 			handleCandidate(msg.sender, msg.data);
   -1   126 		} else {
   -1   127 			console.log('unknown message', msg);
  130   128 		}
  131    -1 		console.log('unknown message', data);
  132   129 	});
  133   130 
  134    -1 	patch.post(room, {type: 'announce', key: queue});
   -1   131 	patch.post(room, {sender: queue, type: 'announce'});
  135   132 
  136   133 	window.addEventListener('beforeunload', function() {
  137    -1 		patch.beacon(room, {type: 'leave', key: queue});
   -1   134 		patch.beacon(room, {sender: queue, type: 'leave'});
  138   135 	});
  139   136 
  140   137 	var updateConnections = function() {
@@ -225,7 +222,7 @@
  225   222 		if (!input.value) {
  226   223 			return;
  227   224 		}
  228    -1 		patch.post(room, {type: 'chat', msg: input.value}).then(function() {
   -1   225 		patch.post(room, {sender: queue, type: 'chat', text: input.value}).then(function() {
  229   226 			input.value = '';
  230   227 		});
  231   228 	});
@@ -237,15 +234,15 @@
  237   234 		history.scrollTop = history.scrollHeight;
  238   235 	};
  239   236 
  240    -1 	patch.listen(room, function(data) {
  241    -1 		if (data.type === 'chat') {
  242    -1 			addChatMsg(data.msg);
  243    -1 		} else if (data.type === 'announce') {
  244    -1 			makeOffer(data.key);
  245    -1 		} else if (data.type === 'leave') {
  246    -1 			closeConnection(data.key);
   -1   237 	patch.listen(room, function(msg) {
   -1   238 		if (msg.type === 'chat') {
   -1   239 			addChatMsg(msg.text);
   -1   240 		} else if (msg.type === 'announce') {
   -1   241 			makeOffer(msg.sender);
   -1   242 		} else if (msg.type === 'leave') {
   -1   243 			closeConnection(msg.sender);
  247   244 		} else {
  248    -1 			console.log('unknown message', data);
   -1   245 			console.log('unknown message', msg);
  249   246 		}
  250   247 	});
  251   248