-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·194 lines (170 loc) · 4.77 KB
/
server.js
File metadata and controls
executable file
·194 lines (170 loc) · 4.77 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const WebSocket = require('ws');
const http = require('http');
const fs = require('fs');
const path = require('path');
// 创建 HTTP 服务器
const server = http.createServer((req, res) => {
// 处理静态文件请求
let filePath = path.join(__dirname, req.url === '/' ? 'index.html' : req.url);
// 获取文件扩展名
const extname = path.extname(filePath);
let contentType = 'text/html';
// 设置内容类型
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
case '.jpeg':
contentType = 'image/jpeg';
break;
case '.gif':
contentType = 'image/gif';
break;
case '.svg':
contentType = 'image/svg+xml';
break;
}
// 读取文件
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
// 文件不存在
res.writeHead(404);
res.end(`File not found: ${req.url}`);
console.log(`404: ${req.url}`);
} else {
// 服务器错误
res.writeHead(500);
res.end(`Server Error: ${error.code}`);
console.error(`Server Error: ${error.code} for ${req.url}`);
}
} else {
// 成功响应
// 设置 no-cache 头部
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
// 创建 WebSocket 服务器
const wss = new WebSocket.Server({ server });
// 存储所有连接的客户端
const clients = new Map();
// WebSocket 连接处理
wss.on('connection', (ws) => {
let userId = null;
console.log('New WebSocket connection');
ws.on('message', (message) => {
try {
const data = JSON.parse(message.toString());
console.log('Received message:', data.type);
// 处理心跳包
if (data.type === '9999') {
ws.send(JSON.stringify({ type: '9999' }));
return;
}
// 处理新用户连接
if (data.type === '1000') {
userId = data.uid || `user_${Math.random().toString(36).substr(2, 9)}`;
clients.set(userId, ws);
// 发送用户ID
ws.send(JSON.stringify({
type: '1001',
data: { id: userId }
}));
console.log(`User connected: ${userId}`);
// 广播用户列表
broadcastUserList();
// 通知用户加入房间
ws.send(JSON.stringify({ type: '1003' }));
return;
}
// 处理ICE候选
if (data.type === '9001') {
const targetWs = clients.get(data.targetId);
if (targetWs) {
targetWs.send(JSON.stringify({
type: '1004',
data: {
targetId: data.uid,
candidate: data.data.candidate
}
}));
}
return;
}
// 处理连接请求
if (data.type === '9002') {
const targetWs = clients.get(data.targetId);
if (targetWs) {
targetWs.send(JSON.stringify({
type: '1005',
data: {
targetId: data.uid,
offer: { sdp: data.data.targetAddr }
}
}));
}
return;
}
// 处理连接响应
if (data.type === '9003') {
const targetWs = clients.get(data.targetId);
if (targetWs) {
targetWs.send(JSON.stringify({
type: '1006',
data: {
targetId: data.uid,
answer: { sdp: data.data.targetAddr }
}
}));
}
return;
}
} catch (error) {
console.error('Error processing message:', error);
}
});
ws.on('close', () => {
if (userId) {
console.log(`User disconnected: ${userId}`);
clients.delete(userId);
broadcastUserList();
}
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
});
// 广播用户列表给所有客户端
function broadcastUserList() {
const userList = Array.from(clients.keys()).map(id => ({ id }));
const message = JSON.stringify({
type: '1002',
data: userList
});
console.log(`Broadcasting user list: ${userList.length} users`);
clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
// 启动服务器
const PORT = process.env.PORT || 8081;
server.listen(PORT, () => {
console.log(`[${new Date().toISOString()}] server start on port ${PORT}`);
});