66 title =" xp@127.0.0.1"
77 center
88 fullscreen
9+ :modal =" false"
10+ :destroy-on-close =" true"
911 @opened =" doOpened"
1012 @open =" doOpen"
1113 @close =" doClose"
12- :modal =" false"
13- :destroy-on-close =" true"
1414 >
1515
16- <div ref =" terminal" ></ div >
16+ <div ref =" terminal" / >
1717
1818 </el-dialog >
1919 </div >
@@ -31,9 +31,9 @@ import 'xterm/dist/xterm.css'
3131// import config from '@/config/config'
3232
3333const defaultTheme = {
34- foreground: ' #ffffff' ,
35- background: ' #1b212f' ,
36- cursor: ' #ffffff' ,
34+ foreground: ' #ffffff' , // 字体
35+ background: ' #1b212f' , // 背景色
36+ cursor: ' #ffffff' , // 设置光标
3737 selection: ' rgba(255, 255, 255, 0.3)' ,
3838 black: ' #000000' ,
3939 brightBlack: ' #808080' ,
@@ -69,8 +69,8 @@ const bindTerminalResize = (term, websocket) => {
6969 term .off (' resize' , onTermResize)
7070 })
7171}
72+
7273const bindTerminal = (term , websocket , bidirectional , bufferedTime ) => {
73- term .write (' \r\n Welcome to Felix Web Ssh Terminal\n\r\n ' )
7474 term .socket = websocket
7575 let messageBuffer = null
7676 const handleWebSocketMessage = function (ev ) {
@@ -96,13 +96,8 @@ const bindTerminal = (term, websocket, bidirectional, bufferedTime) => {
9696 // })
9797 // )
9898 websocket .send (data)
99- console .log (data)
10099
101- if (data === ' \r ' ) {
102- term .write (' \r\n ' + data)
103- } else {
104- term .write (data)
105- }
100+ term .write (data)
106101 }
107102
108103 websocket .onmessage = handleWebSocketMessage
@@ -112,7 +107,8 @@ const bindTerminal = (term, websocket, bidirectional, bufferedTime) => {
112107
113108 // send heartbeat package to avoid closing webSocket connection in some proxy environmental such as nginx.
114109 const heartBeatTimer = setInterval (function () {
115- websocket .send (JSON .stringify ({ type: ' heartbeat' , data: ' ' }))
110+ // websocket.send(JSON.stringify({ type: 'heartbeat', data: '' }))
111+ websocket .send (' ping\r\n ' )
116112 }, 20 * 1000 )
117113
118114 websocket .addEventListener (' close' , function () {
@@ -132,7 +128,9 @@ export default {
132128 v: this .visible ,
133129 ws: null ,
134130 term: null ,
135- thisV: this .visible
131+ thisV: this .visible ,
132+ rows: 35 ,
133+ cols: 100
136134 }
137135 },
138136 computed: {
@@ -177,20 +175,45 @@ export default {
177175 Terminal .applyAddon (fit)
178176 Terminal .applyAddon (webLinks)
179177 Terminal .applyAddon (search)
178+ // 获取容器宽高/字号大小,定义行数和列数
179+ // https://www.cnblogs.com/lhl66/p/7908133.html
180+ // this.rows = document.body.clientHeight / 16 - 5
181+ // this.cols = document.body.clientWidth / 14
182+
180183 this .term = new Terminal ({
181- rows: 35 ,
184+ rendererType: ' canvas' , // 渲染类型
185+ rows: this .rows ,
186+ cols: this .cols ,
187+ convertEol: true , // 启用时,光标将设置为下一行的开头
188+ scrollback: 10 , // 终端中的回滚量
189+ disableStdin: false , // 是否应禁用输入
182190 fontSize: 18 ,
183- cursorBlink: true ,
184- cursorStyle: ' bar' ,
191+ cursorBlink: true , // 光标闪烁
192+ cursorStyle: ' bar' , // 光标样式 underline
185193 bellStyle: ' sound' ,
186194 theme: defaultTheme
187195 })
196+ this .term ._initialized = true
197+ this .term .prompt = () => {
198+ this .term .write (' \r\n ' )
199+ }
200+
201+ this .term .writeln (' Welcome to \x1B [1;3;31mxterm.js\x1B [0m' )
202+ this .term .writeln (' This is a local terminal emulation, without a real terminal in the back-end.' )
203+ this .term .writeln (' Type some keys and commands to play around.' )
204+ this .term .prompt ()
205+
206+ this .term .on (' key' , function (key , ev ) {
207+ console .log (key, ev, ev .keyCode )
208+ })
209+
188210 this .term .open (this .$refs .terminal )
189211 this .term .webLinksInit (this .doLink )
190212 // term.on("resize", this.onTerminalResize);
213+ this .term .on (' resize' , this .onWindowResize )
191214 window .addEventListener (' resize' , this .onWindowResize )
192215 this .term .fit () // first resizing
193- this .ws = new WebSocket (' ws://127.0.0.1:8888/api/ws/ping4 ' )
216+ this .ws = new WebSocket (' ws://127.0.0.1:8888/api/ws/ping5 ' )
194217 this .ws .onerror = () => {
195218 this .$message .error (' ws has no token, please login first' )
196219 this .$router .push ({ name: ' login' })
0 commit comments