EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
agent-redir-rtc-0.1.0.js
Go to the documentation of this file.
1/**
2* @description Mesh Agent Transport Module - using websocket relay
3* @author Ylian Saint-Hilaire
4* @version v0.0.1
5*/
6
7// Construct a MeshServer agent direction object
8var CreateKvmDataChannel = function (webchannel, module, keepalive) {
9 var obj = {};
10 obj.m = module; // This is the inner module (Terminal or Desktop)
11 module.parent = obj;
12 obj.webchannel = webchannel;
13 obj.State = 0;
14 obj.protocol = module.protocol; // 1 = SOL, 2 = KVM, 3 = IDER, 4 = Files, 5 = FileTransfer
15 obj.onStateChanged = null;
16 obj.onControlMsg = null;
17 obj.debugmode = 0;
18 obj.keepalive = keepalive;
19 obj.rtcKeepAlive = null;
20
21 // Private method
22 //obj.debug = function (msg) { console.log(msg); }
23
24 obj.Start = function () {
25 if (obj.debugmode == 1) { console.log('start'); }
26 obj.xxStateChange(3);
27 obj.webchannel.onmessage = obj.xxOnMessage;
28 obj.rtcKeepAlive = setInterval(obj.xxSendRtcKeepAlive, 30000);
29 }
30
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()); } }
40 }
41
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);
56 } else {
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);
61 }
62 } else {
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);
66 }
67 };
68
69 /*
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);
84 } else {
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);
89 }
90 } else {
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);
94 }
95 };
96 */
97
98 obj.xxOnSocketData = function (data) {
99 if (!data) return;
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]); }
104 data = binary;
105 }
106 else if (typeof data !== 'string') return;
107 //console.log("xxOnSocketData", rstr2hex(data));
108 return obj.m.ProcessData(data);
109 }
110
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();
117 }
118 }
119
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);
125 }
126
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);
132 }
133
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);
138 }
139
140 obj.xxSendRtcKeepAlive = function () {
141 //if (urlvars && urlvars['webrtctrace']) { console.log('WebRTC-SendKeepAlive()'); }
142 obj.sendCtrlMsg(JSON.stringify({ action: 'ping' }));
143 }
144
145 return obj;
146}