Files
router/demo/simple/simple-ws/index.html
abearxiong a6a7e74559 feat: Refactor server implementation to support Bun and Node environments
- Introduced `ServerNode` and `BunServer` classes to handle server logic for Node and Bun respectively.
- Updated `App` class to initialize the appropriate server based on the runtime environment.
- Enhanced `parseBody` function to handle request body parsing for both environments.
- Modified WebSocket handling to support Bun's WebSocket upgrade mechanism.
- Improved error handling and response structure across the server implementation.
- Added support for custom middleware in the server's request handling.
- Refactored server base functionality into `ServerBase` for better code organization.
- Updated type definitions to reflect changes in server options and listener handling.
- Added a new demo for testing the server functionality with various request types.
2025-12-20 05:11:51 +08:00

81 lines
2.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket with Redis</title>
</head>
<body>
<h1>Real-time Data Updates</h1>
<p id="output">Waiting for updates...</p>
<script>
// const ws = new WebSocket('ws://localhost:4002/api/router');
const ws = new WebSocket('ws://localhost:4002/api/router');
// const ws = new WebSocket('ws://localhost:4002/ws');
// const ws = new WebSocket('ws://192.168.31.220:4002/api/router');
// 当连接成功时
ws.onopen = () => {
console.log('Connected to WebSocket server');
// 订阅数据 ID 为 1 的更新
// const message = JSON.stringify({ type: 'subscribe', dataId: '1' });
// ws.send(message);
const message = JSON.stringify({
type: 'router',
id: '123556',
data: {
path: 'demo',
key: '01',
}
});
ws.send(message);
// ws.send(JSON.stringify('Hello Server!'));
};
// 接收服务器的消息
ws.onmessage = (event) => {
const parseIfJson = (data) => {
try {
return JSON.parse(data);
} catch (error) {
return data;
}
};
const appendChild = (text) => {
const t = document.createElement('div');
t.innerText = text;
document.body.appendChild(t);
};
console.log('Received:', event.data);
const message = parseIfJson(event.data);
if (typeof message === 'string') {
appendChild(message);
return;
}
if (message.type === 'router') {
const res = message.data;
const text = `Data Updated: ${JSON.stringify(res)}`;
appendChild(text);
console.log('Data updated:', res);
} else {
document.querySelector('#output').innerText = event.data;
console.log('Unknown message type:', message.type);
}
};
// 处理 WebSocket 关闭
ws.onclose = () => {
console.log('Disconnected from WebSocket server');
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
</script>
</body>
</html>