| 96 | } |
| 97 | |
| 98 | connectVisionWS() { |
| 99 | const proto = location.protocol === 'https:' ? 'wss' : 'ws'; |
| 100 | this.ws = new WebSocket(`${proto}://${location.host}/ws/viewer`); // 改为 /ws/viewer |
| 101 | |
| 102 | this.ws.onopen = () => { |
| 103 | console.log('[Vision] WebSocket connected'); |
| 104 | // ... rest of the code |
| 105 | }; |
| 106 | |
| 107 | this.ws.onmessage = (event) => { |
| 108 | // 处理二进制图像数据 |
| 109 | if (event.data instanceof Blob) { |
| 110 | // 创建图像URL并显示 |
| 111 | const url = URL.createObjectURL(event.data); |
| 112 | const img = new Image(); |
| 113 | img.onload = () => { |
| 114 | this.ctx.drawImage(img, 0, 0, this.canvas.width, this.canvas.height); |
| 115 | URL.revokeObjectURL(url); |
| 116 | }; |
| 117 | img.src = url; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | this.ws.onerror = () => { |
| 122 | console.error('Vision WebSocket error'); |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | updateVisualization(data) { |
| 127 | // 更新状态 |