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.
This commit is contained in:
2025-12-20 05:11:51 +08:00
parent e1a53c01ea
commit a6a7e74559
21 changed files with 1173 additions and 454 deletions

View File

@@ -13,7 +13,9 @@
<script>
// const ws = new WebSocket('ws://localhost:4002/api/router');
const ws = new WebSocket('ws://192.168.31.220: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 = () => {
@@ -24,12 +26,14 @@
// 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!'));
};
// 接收服务器的消息
@@ -68,6 +72,9 @@
ws.onclose = () => {
console.log('Disconnected from WebSocket server');
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
</script>
</body>