|
66 | 66 | const scrollToBottomButton = document.getElementById('scroll-to-bottom'); |
67 | 67 | let shouldScrollToBottom = true; |
68 | 68 |
|
| 69 | + // 缓冲区相关 |
| 70 | + let messageBuffer = []; |
| 71 | + let flushScheduled = false; |
| 72 | + const BUFFER_FLUSH_INTERVAL = 100; // 毫秒 |
| 73 | + const BUFFER_MAX_SIZE = 50; // 最大缓冲消息数 |
| 74 | + |
69 | 75 | scrollToBottomButton.addEventListener('click', () => { |
70 | 76 | logContainer.scrollTop = logContainer.scrollHeight; |
71 | 77 | }); |
|
81 | 87 | shouldScrollToBottom = isAtBottom; |
82 | 88 | }); |
83 | 89 |
|
| 90 | + // 刷新缓冲区到 DOM |
| 91 | + function flushBuffer() { |
| 92 | + if (messageBuffer.length === 0) { |
| 93 | + flushScheduled = false; |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // 批量更新文本内容 |
| 98 | + const batchText = messageBuffer.join('\n'); |
| 99 | + logContainer.textContent += '\n' + batchText; |
| 100 | + |
| 101 | + // 检查状态变化(优先级:error > done > start) |
| 102 | + if (batchText.includes('***** ERROR *****')) { |
| 103 | + document.body.className = 'error'; |
| 104 | + } else if (batchText.includes('***** DONE *****')) { |
| 105 | + document.body.className = 'done'; |
| 106 | + } else if (batchText.includes('***** START TRANS *****')) { |
| 107 | + document.body.className = ''; |
| 108 | + } |
| 109 | + |
| 110 | + // 自动滚动 |
| 111 | + if (shouldScrollToBottom) { |
| 112 | + logContainer.scrollTop = logContainer.scrollHeight; |
| 113 | + } |
| 114 | + |
| 115 | + // 清空缓冲区 |
| 116 | + messageBuffer = []; |
| 117 | + flushScheduled = false; |
| 118 | + } |
| 119 | + |
| 120 | + // 调度刷新 |
| 121 | + function scheduleFlush() { |
| 122 | + if (!flushScheduled) { |
| 123 | + flushScheduled = true; |
| 124 | + requestAnimationFrame(() => { |
| 125 | + setTimeout(flushBuffer, BUFFER_FLUSH_INTERVAL); |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // 添加消息到缓冲区 |
| 131 | + function bufferMessage(message) { |
| 132 | + messageBuffer.push(message); |
| 133 | + |
| 134 | + // 如果缓冲区满了,立即刷新 |
| 135 | + if (messageBuffer.length >= BUFFER_MAX_SIZE) { |
| 136 | + if (flushScheduled) { |
| 137 | + // 取消之前的调度,立即刷新 |
| 138 | + flushScheduled = false; |
| 139 | + } |
| 140 | + flushBuffer(); |
| 141 | + } else { |
| 142 | + scheduleFlush(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
84 | 146 | var ws = new ReconnectingWebSocket('ws://' + location.host + '/'); |
85 | 147 | ws.onopen = function () { |
86 | | - logContainer.textContent += '\nWebSocket Connected.'; |
| 148 | + bufferMessage('WebSocket Connected.'); |
87 | 149 | }; |
88 | 150 | ws.onclose = function () { |
89 | | - logContainer.textContent += '\nWebSocket Disconnected.'; |
| 151 | + bufferMessage('WebSocket Disconnected.'); |
90 | 152 | }; |
91 | 153 | ws.onmessage = function (event) { |
92 | | - logContainer.textContent += '\n' + event.data; |
93 | | - if (shouldScrollToBottom) { |
94 | | - logContainer.scrollTop = logContainer.scrollHeight; |
95 | | - } |
96 | | - // 开始/重新开始 |
97 | | - if (event.data.includes('***** START TRANS *****')) { |
98 | | - document.body.className = '' |
99 | | - } |
100 | | - // 错误 |
101 | | - else if (event.data.includes('***** ERROR *****')) { |
102 | | - document.body.className = 'error' |
103 | | - } |
104 | | - // 完成 |
105 | | - else if (event.data.includes('***** DONE *****')) { |
106 | | - document.body.className = 'done' |
107 | | - } |
| 154 | + bufferMessage(event.data); |
108 | 155 | }; |
109 | 156 | </script> |
110 | 157 | </body> |
|
0 commit comments