This commit is contained in:
Annie Huang authored and wancat committed 2023-08-17 19:43:55 +08:00
1 parent 19303aad90
commit dee7e6975e
1 file changed
+50 -86
+50 -86
View File
@@ -13,101 +13,65 @@
</template> </template>
<script setup> <script setup>
import { Terminal } from 'xterm' import { Terminal } from 'xterm'
import { onMounted, ref } from 'vue'
import { FitAddon } from 'xterm-addon-fit' import { FitAddon } from 'xterm-addon-fit'
import { AttachAddon } from 'xterm-addon-attach' import { AttachAddon } from 'xterm-addon-attach'
import { stdin, stdout, exit } from 'process'
import 'xterm/css/xterm.css' import 'xterm/css/xterm.css'
import { io } from 'socket.io-client' import { io } from 'socket.io-client'
// import connect from 'server/src/client' // import connect from 'server/src/client'
import { onMounted, ref } from 'vue'
const rows = ref(40) const rows = ref(40)
const cols = ref(100) const cols = ref(100)
const terminal = ref(null) const terminal = ref(null)
export default {
name: 'TerminalPart', onMounted(() => {
data() { const term = new Terminal({
return { rendererType: 'canvas', // 渲染类型
term: '', // 保存terminal实例 rows: rows.value, // 行数
rows: 40, cols: cols.value, // 不指定行数,自动回车后光标从下一行开始
cols: 100, convertEol: true, // 启用时,光标将设置为下一行的开头
} // scrollback: 50, //终端中的回滚量
}, disableStdin: false, // 是否应禁用输入
mounted() { // cursorStyle: "underline", //光标样式
this.initXterm() cursorBlink: true, // 光标闪烁
}, theme: {
methods: { foreground: '#ECECEC', // 字体
initXterm() { background: '#000000', // 背景色
const xthis = this cursor: 'help', // 设置光标
const term = new Terminal({ lineHeight: 20,
rendererType: 'canvas', // 渲染类型 },
rows: xthis.rows, // 行数 })
cols: xthis.cols, // 不指定行数,自动回车后光标从下一行开始 // const host = 'wss://odyssey.wancat.cc'
convertEol: true, // 启用时,光标将设置为下一行的开头 const host = 'ws://localhost:3000'
// scrollback: 50, //终端中的回滚量 const socket = io(host, {
disableStdin: false, // 是否应禁用输入 query: {
// cursorStyle: "underline", //光标样式 // session_id: sessionId,
cursorBlink: true, // 光标闪烁 session_id: '64d0de0367459c13004bc83f',
theme: { },
foreground: '#ECECEC', // 字体 })
background: '#000000', // 背景色 socket.on('connect', function open() {
cursor: 'help', // 设置光标 console.log('Connected to the server.')
lineHeight: 20, })
}, socket.on('disconnect', function close() {
}) console.log('Disconnected from the server.')
const ws = 'wss://odyssey.wancat.cc' })
// const ws = 'ws://localhost:3000' socket.on('message', (message) => {
const socket = io(ws, { console.log(message)
query: { term.write(message)
// session_id: sessionId, })
session_id: '64cb80132077b982b86bdc4f', term.onData((key) => {
}, console.log(key)
}) socket.send(key)
socket.on('message', function incoming(data) { })
console.log(data) term.open(terminal.value)
term.write(data)
}) const fitAddon = new FitAddon()
socket.on('connect', function open() { term.loadAddon(fitAddon)
console.log('Connected to the server.') fitAddon.fit()
}) function resizeScreen() {
// socket.on('message', function incoming(data) { try {
// stdout.write(data) // 窗口大小改变时,触发xterm的resize方法使自适应
// })
socket.on('disconnect', function close() {
console.log('Disconnected from the server.')
exit()
})
socket.on('message', (message) => {
console.log(message)
term.write(message)
})
term.onData((data) => {
// xthis.socket.emit('data', new TextEncoder().encode(`\x00${data}`))
// socket.send('data', new TextEncoder().encode(`\x00${data}`))
console.log(data)
socket.send(data)
})
// term.onData(function (key) {
// let order = {
// Data: key,
// Op: "stdin"
// };
// xthis.onSend(order);
// });
// xthis.term = term;
// }
// const attachAddon = new AttachAddon(socket)
// 创建terminal实例
// term.loadAddon(attachAddon)
term.open(this.$refs.terminal)
// 换行并输入起始符 $
term.prompt = () => {
term.write('\r\n\x1b[33mLinux Odyssey$\x1b[0m ')
}
term.prompt()
// canvas背景全屏
const fitAddon = new FitAddon()
term.loadAddon(fitAddon)
fitAddon.fit() fitAddon.fit()
} catch (e) { } catch (e) {
console.log('e', e.message) console.log('e', e.message)