第 0 阶段:教材项目 2 任务 1 在网页中原样运行
- runtime:仿真 WinForms 子集(Form、Button、Label、TextBox、ComboBox、 GroupBox、Timer、MessageBox、界面线程消息循环、跨线程检查)、 教材设备类库(DeviceCtrlHelper、LED 控件)、运行器(常驻编译服务 + 运行模式) - server:Bun + Hono,WebSocket 会话,每次运行一个进程,消息转发 - web:Vue + Naive UI,WinForms 风格窗体渲染、MessageBox、实训台 LED 屏 - 端到端测试与 100 个程序并发的压力测试 - 教材逐任务摘要(docs/教材摘要) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
98be8331bc
71 files changed
+7919
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import { Hono } from 'hono'
|
||||
import type { ClientMessage, ServerMessage } from '@iot/shared'
|
||||
import { compileService } from './compile-service'
|
||||
import { cleanupStaleRuns, RunSession } from './run-session'
|
||||
import { listSamples, loadSample } from './samples'
|
||||
|
||||
await cleanupStaleRuns()
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
app.get('/api/samples', async (c) => c.json(await listSamples()))
|
||||
|
||||
app.get('/api/samples/:id', async (c) => {
|
||||
const files = await loadSample(c.req.param('id'))
|
||||
return files ? c.json(files) : c.notFound()
|
||||
})
|
||||
|
||||
interface SocketData {
|
||||
session?: RunSession
|
||||
}
|
||||
|
||||
const server = Bun.serve({
|
||||
port: Number(process.env.PORT ?? 3000),
|
||||
fetch(req, server) {
|
||||
if (new URL(req.url).pathname === '/ws') {
|
||||
if (server.upgrade(req, { data: {} })) return
|
||||
return new Response('WebSocket 升级失败', { status: 400 })
|
||||
}
|
||||
return app.fetch(req)
|
||||
},
|
||||
websocket: {
|
||||
data: {} as SocketData,
|
||||
open(ws) {
|
||||
ws.data.session = new RunSession((message) => ws.send(JSON.stringify(message)))
|
||||
},
|
||||
async message(ws, raw) {
|
||||
let message: ClientMessage
|
||||
try {
|
||||
message = JSON.parse(String(raw))
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
const session = ws.data.session
|
||||
if (!session) return
|
||||
if (message.type === 'run') {
|
||||
try {
|
||||
await session.start(message.files)
|
||||
} catch (error) {
|
||||
ws.send(JSON.stringify({ type: 'serverError', message: String(error) } satisfies ServerMessage))
|
||||
}
|
||||
} else if (message.type === 'stop') {
|
||||
await session.stop()
|
||||
} else {
|
||||
session.input(message)
|
||||
}
|
||||
},
|
||||
async close(ws) {
|
||||
await ws.data.session?.dispose()
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
console.log(`服务器已启动:http://localhost:${server.port}`)
|
||||
await compileService.start()
|
||||
console.log('C# 编译服务已就绪')
|
||||
Reference in new issue
Block a user