This commit is contained in:
Annie Huang authored and wancat committed 2023-08-17 19:43:55 +08:00
1 parent a74233d7f9
commit 19303aad90
2 files changed
+99 -56

No files matched your search

+86 -50
View File
@@ -13,65 +13,101 @@
</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 {
onMounted(() => { name: 'TerminalPart',
const term = new Terminal({ data() {
rendererType: 'canvas', // 渲染类型 return {
rows: rows.value, // 行数 term: '', // 保存terminal实例
cols: cols.value, // 不指定行数,自动回车后光标从下一行开始 rows: 40,
convertEol: true, // 启用时,光标将设置为下一行的开头 cols: 100,
// scrollback: 50, //终端中的回滚量 }
disableStdin: false, // 是否应禁用输入 },
// cursorStyle: "underline", //光标样式 mounted() {
cursorBlink: true, // 光标闪烁 this.initXterm()
theme: { },
foreground: '#ECECEC', // 字体 methods: {
background: '#000000', // 背景色 initXterm() {
cursor: 'help', // 设置光标 const xthis = this
lineHeight: 20, const term = new Terminal({
}, rendererType: 'canvas', // 渲染类型
}) rows: xthis.rows, // 行数
// const host = 'wss://odyssey.wancat.cc' cols: xthis.cols, // 不指定行数,自动回车后光标从下一行开始
const host = 'ws://localhost:3000' convertEol: true, // 启用时,光标将设置为下一行的开头
const socket = io(host, { // scrollback: 50, //终端中的回滚量
query: { disableStdin: false, // 是否应禁用输入
// session_id: sessionId, // cursorStyle: "underline", //光标样式
session_id: '64d0de0367459c13004bc83f', cursorBlink: true, // 光标闪烁
}, theme: {
}) foreground: '#ECECEC', // 字体
socket.on('connect', function open() { background: '#000000', // 背景色
console.log('Connected to the server.') cursor: 'help', // 设置光标
}) lineHeight: 20,
socket.on('disconnect', function close() { },
console.log('Disconnected from the server.') })
}) const ws = 'wss://odyssey.wancat.cc'
socket.on('message', (message) => { // const ws = 'ws://localhost:3000'
console.log(message) const socket = io(ws, {
term.write(message) query: {
}) // session_id: sessionId,
term.onData((key) => { session_id: '64cb80132077b982b86bdc4f',
console.log(key) },
socket.send(key) })
}) socket.on('message', function incoming(data) {
term.open(terminal.value) console.log(data)
term.write(data)
const fitAddon = new FitAddon() })
term.loadAddon(fitAddon) socket.on('connect', function open() {
fitAddon.fit() console.log('Connected to the server.')
function resizeScreen() { })
try { // socket.on('message', function incoming(data) {
// 窗口大小改变时,触发xterm的resize方法使自适应 // stdout.write(data)
// })
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)
+13 -6
View File
@@ -4,12 +4,19 @@ import Session from '../models/session.js'
import { defaultUser, genSessionJWT } from '../utils/auth.js' import { defaultUser, genSessionJWT } from '../utils/auth.js'
export default (server) => { export default (server) => {
const io = new Server(server, { const io = new Server(
cors: { server,
origin: '*', {
cors: {
origin: '*',
},
}, },
}) {
cors: {
origin: '*',
},
}
)
io.use(async (socket, next) => { io.use(async (socket, next) => {
console.log('Authenticating...') console.log('Authenticating...')
try { try {
@@ -54,7 +61,7 @@ export default (server) => {
const stream = await attachContainer(container, { token }) const stream = await attachContainer(container, { token })
stream.on('data', (chunk) => { stream.on('data', (chunk) => {
socket.send(chunk.toString()) socket.send(chunk.toString().toString())
}) })
socket.on('message', function incoming(message) { socket.on('message', function incoming(message) {