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
+66 -23

No files matched your search

+55 -19
View File
@@ -13,23 +13,36 @@
</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',
data() {
return {
term: '', // 保存terminal实例
rows: 40,
cols: 100,
}
},
mounted() {
this.initXterm()
},
methods: {
initXterm() {
const xthis = this
const term = new Terminal({ const term = new Terminal({
rendererType: 'canvas', // 渲染类型 rendererType: 'canvas', // 渲染类型
rows: rows.value, // 行数 rows: xthis.rows, // 行数
cols: cols.value, // 不指定行数,自动回车后光标从下一行开始 cols: xthis.cols, // 不指定行数,自动回车后光标从下一行开始
convertEol: true, // 启用时,光标将设置为下一行的开头 convertEol: true, // 启用时,光标将设置为下一行的开头
// scrollback: 50, //终端中的回滚量 // scrollback: 50, //终端中的回滚量
disableStdin: false, // 是否应禁用输入 disableStdin: false, // 是否应禁用输入
@@ -42,37 +55,60 @@ onMounted(() => {
lineHeight: 20, lineHeight: 20,
}, },
}) })
// const host = 'wss://odyssey.wancat.cc' const ws = 'wss://odyssey.wancat.cc'
const host = 'ws://localhost:3000' // const ws = 'ws://localhost:3000'
const socket = io(host, { const socket = io(ws, {
query: { query: {
// session_id: sessionId, // session_id: sessionId,
session_id: '64d0de0367459c13004bc83f', session_id: '64cb80132077b982b86bdc4f',
}, },
}) })
socket.on('message', function incoming(data) {
console.log(data)
term.write(data)
})
socket.on('connect', function open() { socket.on('connect', function open() {
console.log('Connected to the server.') console.log('Connected to the server.')
}) })
// socket.on('message', function incoming(data) {
// stdout.write(data)
// })
socket.on('disconnect', function close() { socket.on('disconnect', function close() {
console.log('Disconnected from the server.') console.log('Disconnected from the server.')
exit()
}) })
socket.on('message', (message) => { socket.on('message', (message) => {
console.log(message) console.log(message)
term.write(message) term.write(message)
}) })
term.onData((key) => { term.onData((data) => {
console.log(key) // xthis.socket.emit('data', new TextEncoder().encode(`\x00${data}`))
socket.send(key) // socket.send('data', new TextEncoder().encode(`\x00${data}`))
console.log(data)
socket.send(data)
}) })
term.open(terminal.value) // 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() const fitAddon = new FitAddon()
term.loadAddon(fitAddon) term.loadAddon(fitAddon)
fitAddon.fit() fitAddon.fit()
function resizeScreen() {
try {
// 窗口大小改变时,触发xterm的resize方法使自适应
fitAddon.fit()
} catch (e) { } catch (e) {
console.log('e', e.message) console.log('e', e.message)
} }
+11 -4
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(
server,
{
cors: { cors: {
origin: '*', 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) {