forked from james016/distance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (121 loc) · 4.03 KB
/
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
147
148
149
150
151
const WebSocket = require('ws');
const http = require('http');
const server = http.createServer();
const wss = new WebSocket.Server({ server });
// 数据结构
const users = new Map();
const rooms = new Map();
wss.on('connection', (ws) => {
// 生成用户ID
const userId = generateUserId();
users.set(userId, { ws, roomId: null });
console.log(`User ${userId} connected`);
ws.send(JSON.stringify({ type: 'userId', userId }));
console.log(`reply: User ${userId} connected`);
ws.on('message', (message) => {
const data = JSON.parse(message);
switch (data.type) {
case 'joinRoom':
joinRoom(userId, data.roomId, data.position);
break;
case 'leaveRoom':
leaveRoom(userId);
break;
case 'refreshDistances':
refreshDistances(userId, data.position);
break;
default:
break;
}
});
ws.on('close', () => {
leaveRoom(userId);
users.delete(userId);
});
});
// 服务器相关功能函数,如generateUserId、joinRoom、leaveRoom、refreshDistances等
// ...
function generateUserId() {
let userId;
do {
userId = `user${Math.floor(Math.random() * 10000)}`;
} while (users.has(userId));
return userId;
}
function joinRoom(userId, roomId, position) {
const user = users.get(userId);
if (user.roomId) {
leaveRoom(userId);
}
user.roomId = roomId;
user.position = position;
let room = rooms.get(roomId);
if (!room) {
room = new Set();
rooms.set(roomId, room);
}
room.add(userId);
}
function leaveRoom(userId) {
const user = users.get(userId);
if (!user.roomId) return;
const room = rooms.get(user.roomId);
if (room) {
room.delete(userId);
if (room.size === 0) {
rooms.delete(user.roomId);
}
}
user.roomId = null;
// 通知房间内的其他用户
room.forEach((otherUserId) => {
if (otherUserId === userId) return;
const otherUser = users.get(otherUserId);
otherUser.ws.send(JSON.stringify({ type: 'leaveRoom', userId }));
});
}
function refreshDistances(userId, position) {
const user = users.get(userId);
if (!user.roomId) return;
const room = rooms.get(user.roomId);
if (!room) return;
// console.log(`User ${userId} refresh distances, position: ${position.latitude}, ${position.longitude}`);
// 更新用户的位置
user.position = position;
// log all users in the room position
// console.log(
// `position of all users in room ${user.roomId}: ${Array.from(room)
// .map(
// (userId) =>
// users.get(userId).position.latitude +
// "," +
// users.get(userId).position.longitude
// )
// .join("; ")}`
// );
// 计算距离并发送给房间内的所有用户
room.forEach((otherUserId) => {
if (otherUserId === userId) return;
const otherUser = users.get(otherUserId);
const distance = calculateDistance(position, otherUser.position);
user.ws.send(JSON.stringify({ type: 'distance', distance, userId: otherUserId }));
otherUser.ws.send(JSON.stringify({ type: 'distance', distance, userId }));
});
}
function calculateDistance(position1, position2) {
// 使用haversine公式计算地球上两点之间的距离
const R = 6371; // 地球半径(千米)
const lat1 = position1.latitude * (Math.PI / 180);
const lat2 = position2.latitude * (Math.PI / 180);
const dLat = (position2.latitude - position1.latitude) * (Math.PI / 180);
const dLon = (position2.longitude - position1.longitude) * (Math.PI / 180);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c; // 距离(千米)
return distance;
}
server.listen(10002, () => {
console.log('Server is running on port 10002');
});