U.CD.Wetty.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //Wetty主机的部署地址。
  2. U.CD.Wetty.src="http://10.3.15.219:3000";
  3. //执行创建文件,创建主机的api.
  4. U.CD.Wetty.Server="http://10.3.14.30:3001";
  5. Namespace.register("U.CD.Wetty");
  6. U.CD.Wetty.ifameWetty = $$('iframe', {
  7. src: U.CD.Wetty.src,
  8. style: {
  9. cssText: "width: 100%;height: 100%;overflow: hidden;border:none",
  10. },
  11. id: "ifameWetty"
  12. })
  13. /**
  14. * 初始化ifame窗体并执行登陆操作
  15. */
  16. U.CD.Wetty.Init = function () {
  17. U.UI.Form({
  18. title: '终端',
  19. style: {
  20. 'overflow-y': 'hidden',
  21. 'overflow-x': 'hidden',
  22. width: 1010,
  23. height: 700
  24. },
  25. content: U.CD.Wetty.ifameWetty
  26. })
  27. //'<iframe src="http://10.3.14.30:3000" style="width: 100%;height: 100%;overflow: hidden;border:none" id="ifameWetty"></iframe>'
  28. setTimeout(U.CD.Wetty.login, 300)
  29. }
  30. /**
  31. * 通过postMessage方法发送消息至socket.io client
  32. * @param {string} str
  33. */
  34. U.CD.Wetty.send = function (str) {
  35. window.frames['ifameWetty'].contentWindow.postMessage(str, U.CD.Wetty.src);
  36. }
  37. /**
  38. * 提交代码区域内容至后端
  39. */
  40. U.CD.Wetty.run = function () {
  41. var fileName = 'test' + new Date().getTime() + '.c';
  42. var s = $("#UCD_CT_Code_CT")[0].innerText;
  43. if (!U.CD.R.ForbidInput(s)) {
  44. //U.UI.SUForms("暂时不支持输入语句scanf等,请直接赋值,敬请期待下一版", "提示!!!", "UCD_R_InAlert", "color:red;", 300, 200);
  45. U.UI.Confirm("暂时不支持输入语句scanf等,请直接赋值,敬请期待下一版")
  46. return;
  47. }
  48. //如果是Chrome,FireFox,需要过滤掉一些字符
  49. s = U.CD.CD.ExecIFCompChrome(s);
  50. var data = {
  51. user : U.CD.FGuid || 'user', // guid
  52. name: fileName, //
  53. value: s
  54. }
  55. s = s.replace(/\\/g, '\\\\\\\\')
  56. s = s.replace(/(\'|\")/g, '\\\"')
  57. var data2 = "user=" + (U.CD.FGuid || 'user') + "&name=" + fileName + "&value=" + encodeURIComponent(s);
  58. xhr = new XMLHttpRequest();
  59. xhr.open('POST', U.CD.Wetty.Server+'/createFile', true); //加入当前时间以避免缓存
  60. xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  61. xhr.responseType = 'json';
  62. xhr.send(data2);
  63. xhr.onload = function (res) {
  64. if (res.target.response.ret) {
  65. setTimeout(U.CD.Wetty.send('gcc ' + res.target.response.value + ' -o ' + res.target.response.value + '.exe'), 1000)
  66. setTimeout(U.CD.Wetty.send('./' + res.target.response.value + '.exe'), 1000)
  67. }else{
  68. alert(res.target.response.value)
  69. }
  70. };
  71. }
  72. /**
  73. * 根据后端回显执行ssh登陆操作
  74. */
  75. U.CD.Wetty.login = function () {
  76. var ssh = 'ssh root@localhost -p ' + '8000'
  77. var fnList = ['usestudio', 'usestudio-1', ssh, 'yes', '123456']
  78. var index = 0
  79. var fnStart = function () {
  80. if (fnList[index]) {
  81. U.CD.Wetty.send(fnList[index])
  82. console.log(fnList[index]);
  83. index++
  84. if (index === 5) {
  85. window.removeEventListener('message', checkData, false);
  86. U.CD.Wetty.run();
  87. }
  88. }
  89. },
  90. checkData = function (event) {
  91. if (event.data.includes('Password:')) {
  92. fnStart();
  93. }
  94. if (event.data.includes('Welcome to Ubuntu 16.04.2 LTS')) {
  95. fnStart();
  96. }
  97. if (event.data.includes('root@localhost\'s password:')) {
  98. fnStart();
  99. }
  100. }
  101. window.addEventListener('message', checkData, false);
  102. fnStart();
  103. }
  104. /**
  105. * 在容器内创建虚拟机
  106. */
  107. U.CD.Wetty.reg = function () {
  108. U.CD.Ajax.get(U.CD.Wetty.Server+'/createMachine/' + $('#UCD_TOP_L_UI')[0].innerHTML, function (port) {
  109. var ssh = 'ssh root@localhost -p ' + port
  110. var fnList = ['usestudio', 'usestudio-1', ssh, 'yes', '123456']
  111. var index = 0
  112. var fnStart = function () {
  113. if (fnList[index]) {
  114. U.CD.Wetty.send(fnList[index])
  115. index++
  116. } else {
  117. clearInterval(jsq)
  118. }
  119. }
  120. var jsq = setInterval(fnStart, 1000)
  121. })
  122. }
  123. U.CD.Ajax = {
  124. /**
  125. * 发出GET请求并执行回调函数
  126. * @param {string} url
  127. * @param {function} fn
  128. */
  129. get: function (url, fn) {
  130. var obj = new XMLHttpRequest(); // XMLHttpRequest对象用于在后台与服务器交换数据
  131. obj.open('GET', url, true);
  132. obj.onreadystatechange = function () {
  133. if (obj.readyState == 4 && obj.status == 200 || obj.status == 304) { // readyState == 4说明请求已完成
  134. fn.call(this, obj.responseText); //从服务器获得数据
  135. }
  136. };
  137. obj.send();
  138. },
  139. /**
  140. * 发出POST请求并执行回调函数
  141. * @param {string} url
  142. * @param {string} data
  143. * @param {function} fn
  144. */
  145. post: function (url, data, fn) { // datat应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
  146. var obj = new XMLHttpRequest();
  147. obj.open("POST", url, true);
  148. obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // 添加http头,发送信息至服务器时内容编码类型
  149. obj.onreadystatechange = function () {
  150. if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) { // 304未修改
  151. fn.call(this, obj.responseText);
  152. }
  153. };
  154. obj.send(data);
  155. }
  156. }