page8.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. let sshTerm = null;
  2. let sshFit = null;
  3. let sshWS = null;
  4. let sshResizeTimer = null;
  5. function initPage8() {
  6. const el = document.getElementById("ssh-terminal");
  7. if (!el || sshTerm) {
  8. return;
  9. }
  10. sshTerm = new Terminal({
  11. cursorBlink: true,
  12. scrollback: 5000,
  13. fontSize: 14,
  14. convertEol: true,
  15. theme: {
  16. background: "#111111"
  17. }
  18. });
  19. sshFit = new FitAddon.FitAddon();
  20. sshTerm.loadAddon(sshFit);
  21. sshTerm.open(el);
  22. connectSSH();
  23. sshTerm.onData(data => {
  24. if (sshWS?.readyState === WebSocket.OPEN) {
  25. sshWS.send(JSON.stringify({
  26. type: "input",
  27. data: data
  28. }));
  29. }
  30. });
  31. window.addEventListener("resize", resizeSSH);
  32. resizeSSH();
  33. }
  34. function connectSSH() {
  35. sshWS = new WebSocket(
  36. "ws://" + location.host + "/api/ssh/ws"
  37. );
  38. sshWS.onopen = resizeSSH;
  39. sshWS.onmessage = e => {
  40. sshTerm?.write(e.data);
  41. };
  42. sshWS.onclose = () => {
  43. sshWS = null;
  44. };
  45. }
  46. function resizeSSH() {
  47. if (!sshFit || !sshTerm) {
  48. return;
  49. }
  50. clearTimeout(sshResizeTimer);
  51. sshResizeTimer = setTimeout(() => {
  52. sshFit.fit();
  53. if (sshWS?.readyState === WebSocket.OPEN) {
  54. sshWS.send(JSON.stringify({
  55. type: "resize",
  56. cols: sshTerm.cols,
  57. rows: sshTerm.rows
  58. }));
  59. }
  60. }, 100);
  61. }
  62. function exitPage8() {
  63. window.removeEventListener(
  64. "resize",
  65. resizeSSH
  66. );
  67. clearTimeout(sshResizeTimer);
  68. sshWS?.close();
  69. sshWS = null;
  70. sshTerm?.dispose();
  71. sshTerm = null;
  72. sshFit = null;
  73. }