You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.0 KiB

  1. import { broadcast } from '../utils/websocket.js'
  2. export const debugRoutes = (app) => {
  3. const statusList = ['info', 'warn', 'success', 'error', 'finish']
  4. app.post('/api/debug/cmd', (req, res) => {
  5. const { commandId, command, params } = req.body
  6. console.log('收到命令:', command, '参数:', params)
  7. // 模拟返回数据
  8. const mockResponse = {
  9. code: '0',
  10. msg: '成功',
  11. data: null,
  12. }
  13. let messageNum = 0
  14. // 异步广播消息
  15. setTimeout(() => {
  16. res.json(mockResponse)
  17. const poll = setInterval(() => {
  18. if (messageNum === 10) {
  19. clearInterval(poll)
  20. }
  21. broadcast({
  22. type: 'cmd_debug',
  23. data: {
  24. commandId,
  25. command,
  26. status: statusList[Math.floor(Math.random() * statusList.length)],
  27. title: `步骤${messageNum}执行完成`,
  28. content: `具体信息${messageNum}`,
  29. },
  30. })
  31. messageNum++
  32. }, Math.floor(Math.random() * (1000 - 500 + 1)) + 500)
  33. }, 1000)
  34. })
  35. }