Skip to content

Commit 85f6ab8

Browse files
committed
add mock servers
1 parent d6b1af1 commit 85f6ab8

6 files changed

+117
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target
22
.env
33
*.service
4-
**/.DS_Store
4+
**/.DS_Store
5+
**/node_modules

mock-server/http-server.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const http = require('http');
2+
3+
// Create an HTTP server that responds with "Hello, World!" for all requests
4+
const server = http.createServer((req, res) => {
5+
res.writeHead(200, { 'Content-Type': 'text/plain' });
6+
res.end('Hello, World!\n');
7+
});
8+
9+
const PORT = 8081;
10+
server.listen(PORT, () => {
11+
console.log(`Server running at http://localhost:${PORT}/`);
12+
});

mock-server/index.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>WebSocket Client</title>
7+
</head>
8+
<body>
9+
<script>
10+
const socket = new WebSocket('ws://kaichao.localhost:3001');
11+
12+
socket.addEventListener('open', (event) => {
13+
console.log('Connected to the WebSocket server');
14+
});
15+
16+
socket.addEventListener('message', (event) => {
17+
console.log('Received message:', event.data);
18+
});
19+
20+
socket.addEventListener('close', (event) => {
21+
console.log('Connection closed');
22+
});
23+
24+
// Send a message to the server
25+
socket.send('Hello, WebSocket Server!');
26+
</script>
27+
</body>
28+
</html>

mock-server/package-lock.json

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mock-server/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "rlt-ws-server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"ws": "^8.16.0"
13+
}
14+
}

mock-server/websocket-server.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const WebSocket = require('ws');
2+
3+
const server = new WebSocket.Server({ port: 8080 });
4+
5+
server.on('connection', (socket) => {
6+
console.log('Client connected');
7+
8+
// Send a welcome message to the connected client
9+
socket.send('Welcome to the WebSocket server!');
10+
11+
// Listen for messages from the client
12+
socket.on('message', (message) => {
13+
console.log(`Received message: ${message}`);
14+
15+
// Echo the received message back to the client
16+
socket.send(`You said: ${message}`);
17+
});
18+
19+
// Listen for the socket to close
20+
socket.on('close', () => {
21+
console.log('Client disconnected');
22+
});
23+
});
24+
25+
console.log('WebSocket server is running on port 8080');

0 commit comments

Comments
 (0)