• vue xterm4.x自定义请求报文


    1. <script>
    2. import 'xterm/css/xterm.css'
    3. import { Terminal } from 'xterm'
    4. import { FitAddon } from 'xterm-addon-fit'
    5. import { AttachAddon } from 'xterm-addon-attach'
    6. import {getShellUrl,getShellSessionId,fetchBusiPodList,getShellInfo} from '@/api/iresource/workplatform'
    7. import { getToken,get } from '@/utils/auth'
    8. export default {
    9. name: 'Xterm',
    10. props: {
    11. taskId:String,
    12. taskName:String
    13. },
    14. data () {
    15. return {
    16. serverId:"",
    17. listQuery: {
    18. busiId: this.taskId,
    19. busiType: "WorkPlatform",
    20. oneself: 1,
    21. pageNum: 1,
    22. pageSize: 50
    23. },
    24. list: null,
    25. total: null,
    26. totalPage: null,
    27. pageSize: null,
    28. }
    29. },
    30. created() {
    31. console.log("-----shell created-------");
    32. // this.getUrl()
    33. this.getList(); //取参数,请求shell
    34. },
    35. // mounted() {
    36. // console.log("-----shell mounted-------");
    37. // // this.getShellUrlShow();
    38. //
    39. //
    40. // },
    41. beforeDestroy() {
    42. this.socket.close()
    43. this.term.dispose()
    44. },
    45. methods: {
    46. async getList() {
    47. // this.listLoading = true;
    48. console.log(this.listQuery)
    49. fetchBusiPodList(this.listQuery).then(response => {
    50. this.listLoading = false;
    51. console.log("response.data111");
    52. console.log(response.data);
    53. this.list = response.data.items;
    54. // this.list = response.data;
    55. this.total = response.data.total;
    56. this.totalPage = response.data.totalPages;
    57. this.pageSize = response.data.pageSize;
    58. this.showContent();
    59. });
    60. },
    61. async showContent(){
    62. console.log("showContent");
    63. console.log(this.list);
    64. let item = this.list[0]
    65. console.log(item);
    66. console.log("item.busiID");
    67. console.log(item.busiID);
    68. getShellInfo(item.busiID).then(response => {
    69. console.log("getShellInfo");
    70. console.log(response.data);
    71. let item = response.data[0];
    72. this.serverId = response.data.id;
    73. // this.initWebSocket();
    74. //测试2
    75. // let url = "ws://192.168.3.32:32103/iresource/terminal/ws/"+item.namespace+"/"+item.podName+"/"+item.container+"/webshell"
    76. let url = "wss://"+document.location.host+"/shell/iresource/terminal/ws/"+item.namespace+"/"+item.podName+"/"+item.container+"/webshell"
    77. console.log(url);
    78. //测试3,ok
    79. // let url ="ws://127.0.0.1:4001"
    80. this.initSocket(url);
    81. });
    82. },
    83. initSocket(url) {
    84. const term = new Terminal({
    85. fontSize: 14,
    86. cursorBlink: true,
    87. width:'100%',
    88. height:'100%',
    89. });
    90. console.log("term");
    91. console.log(term);
    92. // const attachAddon = new AttachAddon(conn);
    93. const fitAddon = new FitAddon();
    94. // term.loadAddon(attachAddon);
    95. term.loadAddon(fitAddon);
    96. term.open(document.getElementById('xterm'));
    97. // let podName ="cdida9f1ilrqc9buiesg-0";
    98. // term.write("connecting to pod "+ podName + "...")
    99. fitAddon.fit();
    100. term.onData( function (data) {
    101. // term.onData('data', function (data) {
    102. console.log("onData");
    103. console.log(data);
    104. let msg = {operation: "stdin", data: data}
    105. conn.send(JSON.stringify(msg))
    106. });
    107. term.onResize( function (size) {
    108. // term.onData('resize', function (size) {
    109. console.log("resize: " + size)
    110. let msg = {operation: "resize", cols: size.cols, rows: rows}
    111. conn.send(JSON.stringify(msg))
    112. });
    113. // term.focus();
    114. // this.term = term
    115. let conn = new WebSocket(url);
    116. conn.onopen = function(e) {
    117. term.write("\r");
    118. let msg = {operation: "stdin", data: "export TERM=xterm && clear \r"}
    119. conn.send(JSON.stringify(msg))
    120. // term.clear()
    121. };
    122. conn.onmessage = function(event) {
    123. let msg = JSON.parse(event.data)
    124. if (msg.operation === "stdout") {
    125. term.write(msg.data)
    126. } else {
    127. console.log("invalid msg operation: "+msg)
    128. }
    129. };
    130. conn.onclose = function(event) {
    131. if (event.wasClean) {
    132. console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
    133. } else {
    134. console.log('[close] Connection died');
    135. term.writeln("")
    136. }
    137. term.write('Connection Reset By Peer! Try Refresh.');
    138. };
    139. conn.onerror = function(error) {
    140. console.log('[error] Connection error');
    141. term.write("error: "+error.message);
    142. term.destroy();
    143. };
    144. },
    145. }
    146. }
    147. script>

    服务端做了发布:http或https的端口,然后在请求头上增加安全token,接着再转中请求; 

    全屏满屏处理:
    xterm 3.x:
     term.toggleFullScreen(); //全屏 (未测试)

    xterm 4.x:

    1.   setTimeout(() => { //延时满屏
    2.       this.fitAddon.fit()
    3.     }, 60) // 而且不能是0 ,xterm 生成时间在2ms左右

    错误:Vue中 xterm4.x报Uncaught TypeError: r[i].call is not a function

    是没有增加term.onData方法;

    term.onData( function (data) {
    // term.onData('data', function (data) {
        console.log("onData");
        console.log(data);
        let msg = {operation: "stdin", data: data}
        conn.send(JSON.stringify(msg))
    });

    参考:

    xterm.js + vue + websocket实现终端功能(xterm 3.x+xterm 4.x)_仙女爱吃鱼的博客-CSDN博客_xterm.js xterm.js + vue + websocket实现终端功能(xterm 3.x+xterm 4.x)

    xterm.js 基于websocket 链接容器 命令行工具

    通过浏览器连接docker容器_普通网友的博客-CSDN博客_浏览器连接docker 通过浏览器连接docker容器

    vue xterm.js命令行使用及问题解决_潇蓝诺依的博客-CSDN博客_xtermjs vue xterm.js命令行使用及问题解决

    https://blog.csdn.net/wbdisb/article/details/124278817
    Vue父组件调用子组件的方法
    https://blog.csdn.net/qq_31126175/article/details/121608015
    xterm.js 使用及问题【新】

  • 相关阅读:
    React中memo()、useCallback()、useMemo() 区别使用详解
    [2023年]-hadoop面试真题(二)
    js数组按照id删除
    深入浅出学习透析Nginx服务器的基本原理和配置指南「初级实践篇 」
    如何实现网站首页变为黑白色?
    C语言程序设计算法题 -- lab07(1027 - 1030)
    Spring Boot 整合 JSP
    反射的机制
    赛特转债,华懋转债上市价格预测
    【Flutter组件】路由与导航
  • 原文地址:https://blog.csdn.net/hezhenfei8/article/details/127722081