2* @description Mesh Agent Transport Module - using websocket relay
3* @author Ylian Saint-Hilaire
7// Construct a MeshServer agent direction object
8var CreateKvmDataChannel = function (webchannel, module, keepalive) {
10 obj.m = module; // This is the inner module (Terminal or Desktop)
12 obj.webchannel = webchannel;
14 obj.protocol = module.protocol; // 1 = SOL, 2 = KVM, 3 = IDER, 4 = Files, 5 = FileTransfer
15 obj.onStateChanged = null;
16 obj.onControlMsg = null;
18 obj.keepalive = keepalive;
19 obj.rtcKeepAlive = null;
22 //obj.debug = function (msg) { console.log(msg); }
24 obj.Start = function () {
25 if (obj.debugmode == 1) { console.log('start'); }
27 obj.webchannel.onmessage = obj.xxOnMessage;
28 obj.rtcKeepAlive = setInterval(obj.xxSendRtcKeepAlive, 30000);
31 // Setup the file reader
32 var fileReader = new FileReader();
33 var fileReaderInuse = false, fileReaderAcc = [];
34 if (fileReader.readAsBinaryString) {
35 // Chrome & Firefox (Draft)
36 fileReader.onload = function (e) { obj.xxOnSocketData(e.target.result); if (fileReaderAcc.length == 0) { fileReaderInuse = false; } else { fileReader.readAsBinaryString(new Blob([fileReaderAcc.shift()])); } }
37 } else if (fileReader.readAsArrayBuffer) {
38 // Chrome & Firefox (Spec)
39 fileReader.onloadend = function (e) { obj.xxOnSocketData(e.target.result); if (fileReaderAcc.length == 0) { fileReaderInuse = false; } else { fileReader.readAsArrayBuffer(fileReaderAcc.shift()); } }
42 obj.xxOnMessage = function (e) {
43 //if (obj.debugmode == 1) { console.log('Recv', e.data); }
44 //if (urlvars && urlvars['webrtctrace']) { console.log('WebRTC-Recv(' + obj.State + '): ', typeof e.data, e.data); }
45 if (typeof e.data == 'string') { if (obj.onControlMsg != null) { obj.onControlMsg(e.data); } return; } // If this is a control message, handle it here.
46 if (typeof e.data == 'object') {
47 if (fileReaderInuse == true) { fileReaderAcc.push(e.data); return; }
48 if (fileReader.readAsBinaryString) {
49 // Chrome & Firefox (Draft)
50 fileReaderInuse = true;
51 fileReader.readAsBinaryString(new Blob([e.data]));
52 } else if (f.readAsArrayBuffer) {
53 // Chrome & Firefox (Spec)
54 fileReaderInuse = true;
55 fileReader.readAsArrayBuffer(e.data);
57 // IE10, readAsBinaryString does not exist, use an alternative.
58 var binary = '', bytes = new Uint8Array(e.data), length = bytes.byteLength;
59 for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
60 obj.xxOnSocketData(binary);
63 // If we get a string object, it maybe the WebRTC confirm. Ignore it.
64 //obj.debug("Agent Redir Relay - OnData - " + typeof e.data + " - " + e.data.length);
65 obj.xxOnSocketData(e.data);
70 obj.xxOnMessage = function (e) {
71 //if (obj.debugmode == 1) { console.log('Recv', e.data); }
72 //if (urlvars && urlvars['webrtctrace']) { console.log('WebRTC-Recv(' + obj.State + '): ', typeof e.data, e.data); }
73 if (typeof e.data == 'string') { if (obj.onControlMsg != null) { obj.onControlMsg(e.data); } return; } // If this is a control message, handle it here.
74 if (typeof e.data == 'object') {
75 var f = new FileReader();
76 if (f.readAsBinaryString) {
77 // Chrome & Firefox (Draft)
78 f.onload = function (e) { obj.xxOnSocketData(e.target.result); }
79 f.readAsBinaryString(new Blob([e.data]));
80 } else if (f.readAsArrayBuffer) {
81 // Chrome & Firefox (Spec)
82 f.onloadend = function (e) { obj.xxOnSocketData(e.target.result); }
83 f.readAsArrayBuffer(e.data);
85 // IE10, readAsBinaryString does not exist, use an alternative.
86 var binary = '', bytes = new Uint8Array(e.data), length = bytes.byteLength;
87 for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
88 obj.xxOnSocketData(binary);
91 // If we get a string object, it maybe the WebRTC confirm. Ignore it.
92 //obj.debug("Agent Redir Relay - OnData - " + typeof e.data + " - " + e.data.length);
93 obj.xxOnSocketData(e.data);
98 obj.xxOnSocketData = function (data) {
100 if (typeof data === 'object') {
101 // This is an ArrayBuffer, convert it to a string array (used in IE)
102 var binary = '', bytes = new Uint8Array(data), length = bytes.byteLength;
103 for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
106 else if (typeof data !== 'string') return;
107 //console.log("xxOnSocketData", rstr2hex(data));
108 return obj.m.ProcessData(data);
111 // Send a control message over the WebRTC data channel
112 obj.sendCtrlMsg = function (x) {
113 if (typeof x == 'string') {
114 obj.webchannel.send(x);
115 //if (urlvars && urlvars['webrtctrace']) { console.log('WebRTC-Send(' + obj.State + '): ', typeof x, x); }
116 if (obj.keepalive != null) obj.keepalive.sendKeepAlive();
120 // Send a binary message over the WebRTC data channel
121 obj.send = function (x) {
122 if (typeof x == 'string') { var b = new Uint8Array(x.length); for (var i = 0; i < x.length; ++i) { b[i] = x.charCodeAt(i); } x = b; }
123 //if (urlvars && urlvars['webrtctrace']) { console.log('WebRTC-Send(' + obj.State + '): ', typeof x, x); }
124 obj.webchannel.send(x);
127 obj.xxStateChange = function(newstate) {
128 if (obj.State == newstate) return;
129 obj.State = newstate;
130 obj.m.xxStateChange(obj.State);
131 if (obj.onStateChanged != null) obj.onStateChanged(obj, obj.State);
134 obj.Stop = function () {
135 if (obj.debugmode == 1) { console.log('stop'); }
136 if (obj.rtcKeepAlive != null) { clearInterval(obj.rtcKeepAlive); obj.rtcKeepAlive = null; }
137 obj.xxStateChange(0);
140 obj.xxSendRtcKeepAlive = function () {
141 //if (urlvars && urlvars['webrtctrace']) { console.log('WebRTC-SendKeepAlive()'); }
142 obj.sendCtrlMsg(JSON.stringify({ action: 'ping' }));