@@ -27,47 +27,60 @@ <h1>灵心巧手设备列表</h1>
2727 </ table >
2828
2929 < script >
30- const socket = new WebSocket ( 'ws://localhost:18080/ws' ) ;
31- const devices = { } ;
30+ async function initWebSocket ( ) {
31+ try {
32+ const response = await fetch ( '/config?callback=init' ) ;
33+ const configText = await response . text ( ) ;
34+ const configJSON = JSON . parse ( configText . match ( / i n i t \( ( .* ) \) / ) [ 1 ] ) ;
35+ const wsHost = configJSON . host ;
3236
33- socket . onmessage = function ( event ) {
34- const device = JSON . parse ( event . data ) ;
35- const now = new Date ( ) . toLocaleString ( ) ;
36- const deviceKey = device . mac ;
37+ const socket = new WebSocket ( `ws://${ wsHost } /ws` ) ;
38+ const devices = { } ;
3739
38- if ( ! devices [ deviceKey ] ) {
39- devices [ deviceKey ] = {
40- ...device ,
41- firstSeen : now ,
42- lastSeen : now
40+ socket . onmessage = function ( event ) {
41+ const device = JSON . parse ( event . data ) ;
42+ const now = new Date ( ) . toLocaleString ( ) ;
43+ const deviceKey = device . mac ;
44+
45+ if ( ! devices [ deviceKey ] ) {
46+ devices [ deviceKey ] = {
47+ ...device ,
48+ firstSeen : now ,
49+ lastSeen : now
50+ } ;
51+
52+ const row = document . createElement ( 'tr' ) ;
53+ row . id = `device-${ deviceKey } ` ;
54+ row . innerHTML = `
55+ <td>${ device . name } </td>
56+ <td>${ device . ip } </td>
57+ <td>${ device . mac } </td>
58+ <td>${ device . model } </td>
59+ <td>
60+ <a href="https://github.com/linker-bot/can-bridge/releases/tag/v1.2.1/${ device . version } " target="_blank">
61+ ${ device . version }
62+ </a>
63+ </td>
64+ <td>${ now } </td>
65+ <td>${ now } </td>
66+ ` ;
67+ document . getElementById ( 'devices' ) . appendChild ( row ) ;
68+ } else {
69+ devices [ deviceKey ] . lastSeen = now ;
70+ const row = document . getElementById ( `device-${ deviceKey } ` ) ;
71+ row . cells [ 6 ] . textContent = now ;
72+ }
4373 } ;
4474
45- const row = document . createElement ( 'tr' ) ;
46- row . id = `device-${ deviceKey } ` ;
47- row . innerHTML = `
48- <td>${ device . name } </td>
49- <td>${ device . ip } </td>
50- <td>${ device . mac } </td>
51- <td>${ device . model } </td>
52- <td>
53- <a href="https://github.com/linker-bot/can-bridge/releases/tag/v1.2.1/${ device . version } " target="_blank">
54- ${ device . version }
55- </a>
56- </td>
57- <td>${ now } </td>
58- <td>${ now } </td>
59- ` ;
60- document . getElementById ( 'devices' ) . appendChild ( row ) ;
61- } else {
62- devices [ deviceKey ] . lastSeen = now ;
63- const row = document . getElementById ( `device-${ deviceKey } ` ) ;
64- row . cells [ 6 ] . textContent = now ;
75+ socket . onerror = function ( event ) {
76+ console . error ( "WebSocket 错误:" , event ) ;
77+ } ;
78+ } catch ( error ) {
79+ console . error ( "配置初始化失败:" , error ) ;
6580 }
66- } ;
81+ }
6782
68- socket . onerror = function ( event ) {
69- console . error ( "WebSocket 错误:" , event ) ;
70- } ;
83+ initWebSocket ( ) ;
7184 </ script >
7285</ body >
7386</ html >
0 commit comments