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