码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 2023 ~【VUE+Xterm+Websocket】模拟SSH连接效果


    1、安装包 xterm、 xterm-addon-attach、 xterm-addon-fit

    cnpm install xterm --save
    
    • 1
    cnpm install xterm-addon-attach --save
    
    • 1
    cnpm install xterm-addon-fit --save
    
    • 1

    安装最新版本即可

    "xterm": "^5.2.1",
    "xterm-addon-attach": "^0.8.0",
    "xterm-addon-fit": "^0.7.0"
    
    • 1
    • 2
    • 3

    2、在页面中使用

    <div id="terBox" ref="terminal" style="padding-top: 5px; height: calc(100vh - 294px)" />
    
    • 1
    <script>
    import "xterm/css/xterm.css";
    import { Terminal } from "xterm";
    import { FitAddon } from "xterm-addon-fit"; // 全屏
    import { AttachAddon } from "xterm-addon-attach";
    
    export default {
      	name: "Ssh",
    	data() {
    	    return {
    	      term: null,
    	      socket: "",
    	    };
    	},
    	mounted() {
    	  this.initTerm();
    	  window.addEventListener("resize", this.resizeScreen);
    	},
    	methods: {
    	    initTerm() {
    	      this.$nextTick(() => {
    	        var rowHeight = document.getElementById("terBox").clientHeight; // 高
    	        // 1.xterm终端初始化
    	        let term = new Terminal({
    	          rendererType: "canvas", //渲染类型
    	          rows: Math.trunc(rowHeight / 17 - 1)  || 36, //行数
    	          // cols: 10, // 不指定行数,自动回车后光标从下一行开始
    	          convertEol: true, //启用时,光标将设置为下一行的开头
    	          // scrollback: 50, //终端中的回滚量
    	          disableStdin: false, //是否应禁用输入
    	          windowsMode: true, // 根据窗口换行
    	          cursorStyle: "underline", //光标样式
    	          cursorBlink: true, //光标闪烁
    	          theme: {
    	            foreground: "#ECECEC", //字体
    	            background: "#000000", //背景色
    	            cursor: "help", //设置光标
    	            lineHeight: 20,
    	          },
    	        });
    	
    	        // 2.webSocket初始化 (不需要另外的方法)
    	        const socketUri = "ws://" + process.env.VUE_APP_SOCKET + ":9207/sshSocket";
    	        this.socket = new WebSocket(socketUri); // 发起连接
    	
    	        // 3.websocket集成的插件
    	        const attachAddon = new AttachAddon(this.socket);
    	        const fitAddon = new FitAddon(); // 全屏插件
    	        term.loadAddon(attachAddon);
    	        term.loadAddon(fitAddon);
    	        term.open(this.$refs.terminal);
    	        fitAddon.fit();
    	        term.focus();
    	        this.term = term;
    	        // 发送 term 数据
    	        this.term.onData((data) => {
    	          this.sendSSH({
    	            operate: "command",
    	            host: this.loginForm.host,
    	            port: this.loginForm.port || 22,
    	            command: data,
    	            rows: this.term.rows,
    	            cols: this.term.cols,
    	          }); // 初次连接SSH
    	        });
    	      });
    	    },
    	    /** 当屏幕变化时修改 */
    	    resizeScreen() {
    	      var colWidth = document.getElementById("terBox").clientWidth; // 宽
    	      var rowHeight = document.getElementById("terBox").clientHeight; // 高
    	      // 注意修改 用 term.resize(cols, rows),不能用 term.cols = cols(只能取值)
    	      this.term.resize(Math.trunc(colWidth / 9), Math.trunc(rowHeight / 17 - 1)) // 修改 cols 和 rows
    	    },
    	}
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    解决方案 | 如何构建市政综合管廊安全运行监测系统?
    [架构之路-51]:架构师 - 用系统化、结构化思维解决复杂难搞的软件故障问题 - 马克思主义哲学在软件系统中的应用
    Java进阶(JVM调优)——阿里云的Arthas的使用 & 安装和使用 & 死锁查找案例,重新加载案例,慢调用分析
    中国冰淇淋市场深度评估及发展趋势预测报告(2022版)
    AutoCAD Civil 3D中文本地化模板下载及设置
    Java开发:反射机制
    Knowledge Editing for Large Language Models: A Survey
    内网渗透-【横向移动】PsExec工具远程命令执行横向移动
    DEFORMABLE DETR: DEFORMABLE TRANSFORMERS FOR END-TO-END OBJECT DETECTION (论文解析)
    Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
  • 原文地址:https://blog.csdn.net/sunshineTing2/article/details/132627647
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号