You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.js 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. function openTerminal(options) {
  2. //var CONNECT_TIME = 0; // 请求连接次数
  3. $("#form").hide();
  4. Rows = parseInt(options.Rows);
  5. var client = new WSSHClient();
  6. var base64 = new Base64();
  7. var term = new Terminal({cols: 80, rows: Rows, screenKeys: true, useStyle: true});
  8. // 发送客户端数据
  9. term.on('data', function (data) {
  10. console.log("xterm data: ");
  11. console.log(data);
  12. client.sendClientData(data);
  13. });
  14. term.open();
  15. $('.terminal').detach().appendTo('#term');
  16. $("#term").show();
  17. term.write("Connecting...");
  18. console.debug(options);
  19. //var interTime = setInterval(client_connect, 1000)
  20. setTimeout(client_connect, 3000);
  21. var intervalId = null;
  22. function client_connect() {
  23. // var TIMEINIT = 0; // 初始化时间
  24. // var TIMEOUT = 60 * 15; // 超时时间
  25. var CONNECTED = false; // 是否连接成功过
  26. console.log("连接中....");
  27. console.log(options);
  28. client.connect({
  29. onError: function (error) {
  30. term.write('Error: ' + error + '\r\n');
  31. console.log('error happened');
  32. },
  33. onConnect: function () {
  34. console.log('connection established');
  35. // 连接上之后发送初始化数据
  36. client.sendInitData(options);
  37. term.focus();
  38. },
  39. onClose: function () {
  40. console.log("连接关闭");
  41. term.write("\r\nconnection closed");
  42. if (CONNECTED) {
  43. console.log('connection reset by peer');
  44. $('term').hide();
  45. }
  46. },
  47. // 当收到服务端返回的数据
  48. onData: function (data) {
  49. if (!CONNECTED) {
  50. console.log("first connected.");
  51. term.write("\r"); //换行
  52. term.focus(); //焦点移动到框上
  53. }
  54. /*if(interTime){
  55. clearInterval(interTime);
  56. }*/
  57. CONNECTED = true;
  58. data = base64.decode(data);
  59. /* TIMEINIT = 0;*/
  60. term.write(data);
  61. console.log('get data:' + data);
  62. }
  63. })
  64. }
  65. }
  66. var charWidth = 6.2;
  67. var charHeight = 15.2;
  68. /**
  69. * for full screen
  70. * @returns {{w: number, h: number}}
  71. */
  72. function getTerminalSize() {
  73. var width = window.innerWidth;
  74. var height = window.innerHeight;
  75. return {
  76. w: Math.floor(width / charWidth),
  77. h: Math.floor(height / charHeight)
  78. };
  79. }
  80. function store(options) {
  81. window.localStorage.host = options.host
  82. window.localStorage.port = options.port
  83. window.localStorage.username = options.username
  84. window.localStorage.ispwd = options.ispwd
  85. window.localStorage.password = options.password
  86. }
  87. function check() {
  88. return validResult["host"] && validResult["port"] && validResult["username"];
  89. }
  90. function connect() {
  91. var remember = $("#remember").is(":checked")
  92. var options = {
  93. host: $("#host").val(),
  94. port: $("#port").val(),
  95. username: $("#username").val(),
  96. password: $("#password").val(),
  97. Rows: $("#terminalRow").val(),
  98. }
  99. if (remember) {
  100. store(options)
  101. }
  102. if (true) {
  103. openTerminal(options)
  104. } else {
  105. for (var key in validResult) {
  106. if (!validResult[key]) {
  107. alert(errorMsg[key]);
  108. break;
  109. }
  110. }
  111. }
  112. }

No Description

Contributors (1)