Skip to content

Commit 5bbd3e8

Browse files
committed
feat: init with config api
1 parent 063e2d8 commit 5bbd3e8

File tree

2 files changed

+64
-35
lines changed

2 files changed

+64
-35
lines changed

main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ func main() {
9595
c.String(200, "pong")
9696
})
9797

98+
r.GET("/config", func(c *gin.Context) {
99+
callback := c.Query("callback")
100+
host := c.Request.Host
101+
102+
if callback == "" {
103+
c.JSON(400, gin.H{"error": "callback parameter required"})
104+
return
105+
}
106+
107+
data := gin.H{
108+
"host": host,
109+
}
110+
111+
c.JSONP(200, data)
112+
})
113+
98114
r.NoRoute(func(c *gin.Context) {
99115
fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path)
100116
c.Redirect(http.StatusMovedPermanently, "/")

public/index.html

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/init\((.*)\)/)[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

Comments
 (0)