Made it easier to debug log WebRTC/WebSocket

This commit is contained in:
Ylian Saint-Hilaire 2021-11-18 12:00:45 -08:00
parent 05ee363a66
commit 17913da65f
3 changed files with 20 additions and 5 deletions

View File

@ -2968,7 +2968,7 @@ function onTunnelControlData(data, ws) {
ws.webrtc.on('disconnected', function () { /*sendConsoleText('Tunnel #' + this.websocket.tunnel.index + ' WebRTC disconnected');*/ });
ws.webrtc.on('dataChannel', function (rtcchannel) {
//sendConsoleText('WebRTC Datachannel open, protocol: ' + this.websocket.httprequest.protocol);
rtcchannel.maxFragmentSize = 32768;
//rtcchannel.maxFragmentSize = 32768;
rtcchannel.xrtc = this;
rtcchannel.websocket = this.websocket;
this.rtcchannel = rtcchannel;

View File

@ -55,7 +55,7 @@ var CreateKvmDataChannel = function (webchannel, module, keepalive) {
fileReader.readAsArrayBuffer(e.data);
} else {
// IE10, readAsBinaryString does not exist, use an alternative.
var binary = "", bytes = new Uint8Array(e.data), length = bytes.byteLength;
var binary = '', bytes = new Uint8Array(e.data), length = bytes.byteLength;
for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
obj.xxOnSocketData(binary);
}
@ -99,7 +99,7 @@ var CreateKvmDataChannel = function (webchannel, module, keepalive) {
if (!data) return;
if (typeof data === 'object') {
// This is an ArrayBuffer, convert it to a string array (used in IE)
var binary = "", bytes = new Uint8Array(data), length = bytes.byteLength;
var binary = '', bytes = new Uint8Array(data), length = bytes.byteLength;
for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
data = binary;
}

View File

@ -43,6 +43,18 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
// Private method
//obj.debug = function (msg) { console.log(msg); }
// Display websocket or webrtc data to the console
function logData(e, name) {
if (typeof e.data == 'object') {
var view = new Uint8Array(e.data), cmd = (view[0] << 8) + view[1], cmdsize = (view[2] << 8) + view[3];
console.log(name + ' binary data', cmd, cmdsize, e.data.byteLength, buf2hex(e.data).substring(0, 24));
} else if (typeof e.data == 'string') {
console.log(name + ' string data', e.data.length, e.data);
} else {
console.log(name + ' unknown data', e.data);
}
}
obj.Start = function (nodeid) {
var url2, url = window.location.protocol.replace('http', 'ws') + '//' + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/' + obj.urlname + '?browser=1&p=' + obj.protocol + (nodeid?('&nodeid=' + nodeid):'') + '&id=' + obj.tunnelid;
//if (serverPublicNamePort) { url2 = window.location.protocol.replace('http', 'ws') + '//' + serverPublicNamePort + '/meshrelay.ashx?id=' + obj.tunnelid; } else { url2 = url; }
@ -54,7 +66,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
obj.socket.binaryType = 'arraybuffer';
obj.socket.onopen = obj.xxOnSocketConnected;
obj.socket.onmessage = obj.xxOnMessage;
//obj.socket.onmessage = function (e) { console.log('Websocket data', e.data); obj.xxOnMessage(e); }
//obj.socket.onmessage = function (e) { logData(e, 'WebSocket'); obj.xxOnMessage(e); }
obj.socket.onerror = function (e) { /* console.error(e); */ }
obj.socket.onclose = obj.xxOnSocketClosed;
obj.xxStateChange(1);
@ -140,7 +152,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
obj.webchannel = obj.webrtc.createDataChannel('DataChannel', {}); // { ordered: false, maxRetransmits: 2 }
obj.webchannel.binaryType = 'arraybuffer';
obj.webchannel.onmessage = obj.xxOnMessage;
//obj.webchannel.onmessage = function (e) { console.log('WebRTC data', e.data); obj.xxOnMessage(e); }
//obj.webchannel.onmessage = function (e) { logData(e, 'WebRTC'); obj.xxOnMessage(e); }
obj.webchannel.onopen = function () { obj.webRtcActive = true; performWebRtcSwitch(); };
obj.webchannel.onclose = function (event) { if (obj.webRtcActive) { obj.Stop(); } }
obj.webrtc.onicecandidate = function (e) {
@ -288,5 +300,8 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
obj.xxStateChange(0);
}
// Buffer is an ArrayBuffer
function buf2hex(buffer) { return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join(''); }
return obj;
}