agent-redir-rtc-0.1.0.js 6.5 KB

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