mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: push discover event to frontend
This commit is contained in:
1 parent
c0186c6a30
commit
096b7a7714
5 files changed
+33
-3
No files matched your search
@@ -72,6 +72,11 @@ async function connect(sessionId) {
|
||||
stdout.write(data)
|
||||
})
|
||||
|
||||
socket.on('graph', (data) => {
|
||||
console.log('receive graph:')
|
||||
console.log(data)
|
||||
})
|
||||
|
||||
socket.on('close', function close() {
|
||||
console.log('Disconnected from the server.')
|
||||
exit()
|
||||
|
||||
@@ -33,7 +33,6 @@ async function collectFilesInfo(inputPath, level = 0, hiddenFiles = false) {
|
||||
}
|
||||
|
||||
async function discoverFiles(argv) {
|
||||
console.log(argv)
|
||||
const targetPath = argv._.length < 2 ? ['.'] : argv._.slice(1)
|
||||
const result = await Promise.all(
|
||||
targetPath
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function newCommand(req, res) {
|
||||
|
||||
await c.save()
|
||||
|
||||
const response = await commandHandler(session, c)
|
||||
const response = await commandHandler(session, c, additionalData)
|
||||
console.log(response)
|
||||
|
||||
res.status(201).json(response)
|
||||
|
||||
@@ -3,6 +3,20 @@ import { getOrCreateContainer, attachContainer } from '../utils/docker.js'
|
||||
import Session from '../models/session.js'
|
||||
import { defaultUser, genSessionJWT } from '../utils/auth.js'
|
||||
|
||||
const sessions = new Map()
|
||||
|
||||
function listenToSession(sessionId, event, callback) {
|
||||
sessions.set((sessionId, event), callback)
|
||||
}
|
||||
|
||||
export function pushToSession(sessionId, event, data) {
|
||||
console.log(sessions, sessionId, event, data)
|
||||
const callback = sessions.get((sessionId, event))
|
||||
if (callback) {
|
||||
callback(data)
|
||||
}
|
||||
}
|
||||
|
||||
export default (server) => {
|
||||
const io = new Server(server)
|
||||
|
||||
@@ -63,6 +77,10 @@ export default (server) => {
|
||||
console.log('Disconnected from the client.')
|
||||
})
|
||||
|
||||
listenToSession(session.id, 'graph', (data) => {
|
||||
socket.emit('graph', data)
|
||||
})
|
||||
|
||||
socket.send(`${container.id}\n`)
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import Quest from '../models/quest.js'
|
||||
import { pushToSession } from '../api/socket.js'
|
||||
|
||||
// 檢查 pattern 是否符合 input
|
||||
const checkMatch = (pattern, input) => {
|
||||
@@ -9,7 +10,7 @@ const checkMatch = (pattern, input) => {
|
||||
})
|
||||
}
|
||||
|
||||
const commandHandler = async (session, commandInput) => {
|
||||
async function commandHandler(session, commandInput, additionalData) {
|
||||
const quest = await Quest.findById(session.quest)
|
||||
const stage = quest.stages.find((s) => s.id === session.progress)
|
||||
if (!stage) {
|
||||
@@ -17,6 +18,13 @@ const commandHandler = async (session, commandInput) => {
|
||||
return {}
|
||||
}
|
||||
|
||||
if ('discover' in additionalData) {
|
||||
pushToSession(session.id, 'graph', {
|
||||
pwd: commandInput.pwd,
|
||||
discover: additionalData.discover,
|
||||
})
|
||||
}
|
||||
|
||||
const commandMatch = checkMatch(stage.condition.command, commandInput.command)
|
||||
const outputMatch = checkMatch(stage.condition.output, commandInput.output)
|
||||
const errorMatch = checkMatch(stage.condition.error, commandInput.output)
|
||||
|
||||
Reference in new issue
Block a user