123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //Wetty主机的部署地址。
- U.CD.Wetty.src="http://10.3.15.219:3000";
- //执行创建文件,创建主机的api.
- U.CD.Wetty.Server="http://10.3.14.30:3001";
- Namespace.register("U.CD.Wetty");
- U.CD.Wetty.ifameWetty = $$('iframe', {
- src: U.CD.Wetty.src,
- style: {
- cssText: "width: 100%;height: 100%;overflow: hidden;border:none",
- },
- id: "ifameWetty"
- })
- /**
- * 初始化ifame窗体并执行登陆操作
- */
- U.CD.Wetty.Init = function () {
- U.UI.Form({
- title: '终端',
- style: {
- 'overflow-y': 'hidden',
- 'overflow-x': 'hidden',
- width: 1010,
- height: 700
- },
- content: U.CD.Wetty.ifameWetty
- })
- //'<iframe src="http://10.3.14.30:3000" style="width: 100%;height: 100%;overflow: hidden;border:none" id="ifameWetty"></iframe>'
- setTimeout(U.CD.Wetty.login, 300)
- }
- /**
- * 通过postMessage方法发送消息至socket.io client
- * @param {string} str
- */
- U.CD.Wetty.send = function (str) {
- window.frames['ifameWetty'].contentWindow.postMessage(str, U.CD.Wetty.src);
- }
- /**
- * 提交代码区域内容至后端
- */
- U.CD.Wetty.run = function () {
- var fileName = 'test' + new Date().getTime() + '.c';
- var s = $("#UCD_CT_Code_CT")[0].innerText;
- if (!U.CD.R.ForbidInput(s)) {
- //U.UI.SUForms("暂时不支持输入语句scanf等,请直接赋值,敬请期待下一版", "提示!!!", "UCD_R_InAlert", "color:red;", 300, 200);
- U.UI.Confirm("暂时不支持输入语句scanf等,请直接赋值,敬请期待下一版")
- return;
- }
- //如果是Chrome,FireFox,需要过滤掉一些字符
- s = U.CD.CD.ExecIFCompChrome(s);
- var data = {
- user : U.CD.FGuid || 'user', // guid
- name: fileName, //
- value: s
- }
- s = s.replace(/\\/g, '\\\\\\\\')
- s = s.replace(/(\'|\")/g, '\\\"')
- var data2 = "user=" + (U.CD.FGuid || 'user') + "&name=" + fileName + "&value=" + encodeURIComponent(s);
- xhr = new XMLHttpRequest();
- xhr.open('POST', U.CD.Wetty.Server+'/createFile', true); //加入当前时间以避免缓存
- xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
- xhr.responseType = 'json';
- xhr.send(data2);
- xhr.onload = function (res) {
- if (res.target.response.ret) {
- setTimeout(U.CD.Wetty.send('gcc ' + res.target.response.value + ' -o ' + res.target.response.value + '.exe'), 1000)
- setTimeout(U.CD.Wetty.send('./' + res.target.response.value + '.exe'), 1000)
- }else{
- alert(res.target.response.value)
- }
- };
- }
- /**
- * 根据后端回显执行ssh登陆操作
- */
- U.CD.Wetty.login = function () {
- var ssh = 'ssh root@localhost -p ' + '8000'
- var fnList = ['usestudio', 'usestudio-1', ssh, 'yes', '123456']
- var index = 0
- var fnStart = function () {
- if (fnList[index]) {
- U.CD.Wetty.send(fnList[index])
- console.log(fnList[index]);
- index++
- if (index === 5) {
- window.removeEventListener('message', checkData, false);
- U.CD.Wetty.run();
- }
- }
- },
- checkData = function (event) {
- if (event.data.includes('Password:')) {
- fnStart();
- }
- if (event.data.includes('Welcome to Ubuntu 16.04.2 LTS')) {
- fnStart();
- }
- if (event.data.includes('root@localhost\'s password:')) {
- fnStart();
- }
- }
- window.addEventListener('message', checkData, false);
- fnStart();
- }
- /**
- * 在容器内创建虚拟机
- */
- U.CD.Wetty.reg = function () {
- U.CD.Ajax.get(U.CD.Wetty.Server+'/createMachine/' + $('#UCD_TOP_L_UI')[0].innerHTML, function (port) {
- var ssh = 'ssh root@localhost -p ' + port
- var fnList = ['usestudio', 'usestudio-1', ssh, 'yes', '123456']
- var index = 0
- var fnStart = function () {
- if (fnList[index]) {
- U.CD.Wetty.send(fnList[index])
- index++
- } else {
- clearInterval(jsq)
- }
- }
- var jsq = setInterval(fnStart, 1000)
- })
- }
- U.CD.Ajax = {
- /**
- * 发出GET请求并执行回调函数
- * @param {string} url
- * @param {function} fn
- */
- get: function (url, fn) {
- var obj = new XMLHttpRequest(); // XMLHttpRequest对象用于在后台与服务器交换数据
- obj.open('GET', url, true);
- obj.onreadystatechange = function () {
- if (obj.readyState == 4 && obj.status == 200 || obj.status == 304) { // readyState == 4说明请求已完成
- fn.call(this, obj.responseText); //从服务器获得数据
- }
- };
- obj.send();
- },
- /**
- * 发出POST请求并执行回调函数
- * @param {string} url
- * @param {string} data
- * @param {function} fn
- */
- post: function (url, data, fn) { // datat应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
- var obj = new XMLHttpRequest();
- obj.open("POST", url, true);
- obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // 添加http头,发送信息至服务器时内容编码类型
- obj.onreadystatechange = function () {
- if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) { // 304未修改
- fn.call(this, obj.responseText);
- }
- };
- obj.send(data);
- }
- }
|