-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_server.js
146 lines (115 loc) · 3.94 KB
/
websocket_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// const WebSocket = require('ws');
// // 创建WebSocket服务器实例,监听3000端口
// const wss = new WebSocket.Server({ port: 3000 });
// // 监听连接事件
// wss.on('connection', function connection(ws) {
// console.log('Client connected');
// // 监听消息事件
// ws.on('message', function incoming(message) {
// console.log('received: %s', message);
// // 收到消息后将其原样发送回客户端
// ws.send(message);
// });
// // 监听关闭事件
// ws.on('close', function close() {
// console.log('Client disconnected');
// });
// });
// console.log('WebSocket server running at ws://localhost:3000/');
// const WebSocket = require("ws");
// const wss = new WebSocket.Server({ port: 3000 });
// wss.on("connection", function connection(ws) {
// console.log("Client connected");
// ws.on("message", function incoming(message) {
// console.log("received: %s", message);
// // 解析消息
// let parsedMessage;
// try {
// parsedMessage = JSON.parse(message);
// } catch (error) {
// console.error("Error parsing message:", error);
// return;
// }
// // 处理消息(示例:如果收到的是心跳消息,则忽略)
// if (parsedMessage.type === "heartbeat") {
// console.log("Received heartbeat");
// return;
// }
// // 发送消息给客户端
// ws.send(JSON.stringify({ type: "chat", text: parsedMessage.text }));
// });
// ws.on("close", function close() {
// console.log("Client disconnected");
// });
// });
// console.log("WebSocket server running at ws://localhost:3000/");
const WebSocket = require("ws");
const HEARTBEAT_INTERVAL = 5000; // 心跳间隔(毫秒)
const CLIENT_TIMEOUT = 15000; // 客户端超时时间(毫秒)
const wss = new WebSocket.Server({ port: 3000 });
wss.on("connection", function connection(ws) {
console.log("Client connected");
let heartbeatInterval;
let messageCounter = 0;
const intervalId = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
const message = JSON.stringify({ type: "msg", text: messageCounter });
ws.send(message);
messageCounter++;
} else {
clearInterval(intervalId);
}
}, 2000); // 每2秒发送一条消息
// 客户端心跳检测
function heartbeat() {
clearInterval(heartbeatInterval);
heartbeatInterval = setInterval(() => {
if (ws.isAlive === false) {
// console.log("Client is not responding. Attempting to reconnect...");
console.log("Client is not responding.");
// 如果客户端由于网络,宕机等原因停止,服务端就停止wsocket连接
return ws.terminate();
} else {
// 如果客户端仍然存活,发送心跳消息
ws.isAlive = false;
ws.ping();
console.log("ping~~");
}
}, HEARTBEAT_INTERVAL);
}
ws.isAlive = true;
// 监听来自客户端的消息
ws.on("message", function incoming(message) {
console.log("received: %s", message);
// 解析消息
let parsedMessage;
try {
parsedMessage = JSON.parse(message);
} catch (error) {
console.error("Error parsing message:", error);
return;
}
// 处理消息(示例:如果收到的是心跳消息,则更新客户端状态)
if (parsedMessage.type === "heartbeat") {
// console.log("Received heartbeat");
ws.isAlive = true;
return;
}
// 发送消息给客户端
ws.send(JSON.stringify({ type: "chat", text: parsedMessage.text }));
});
// 监听心跳消息
ws.on("pong", function heartbeatResponse() {
console.log("pong received");
ws.isAlive = true;
});
// 启动心跳检测
heartbeat();
// 监听连接关闭事件
ws.on("close", function close() {
console.log("Client disconnected");
clearInterval(heartbeatInterval);
clearInterval(intervalId);
});
});
console.log("WebSocket server running at ws://localhost:3000/");