forked from networked-aframe/networked-aframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuws-server.cjs
239 lines (205 loc) · 6.46 KB
/
uws-server.cjs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Verify the latest version of uWebSockets.js at https://github.com/uNetworking/uWebSockets.js
// Run:
// npm install uNetworking/uWebSockets.js#v20.51.0
// and start the server:
// node server/uws-server.cjs
// To use the uws adapter, specify it in your index.html networked-scene:
// adapter: uws;
// You can also remove any socket.io and easyrtc script tags from your index.html
// as they are not needed with uws.
const uWS = require("uWebSockets.js");
const path = require("path");
const fs = require("fs");
// Set process name
process.title = "networked-aframe-server";
// If you use nginx in front, comment uWS.SSLApp and use uWS.App
// and in your index.html networked-scene:
// adapter: uws;
// serverURL: /uws;
// and in your nginx.conf:
// location /uws {
// proxy_pass http://127.0.0.1:8080;
// proxy_http_version 1.1;
// proxy_set_header Upgrade $http_upgrade;
// proxy_set_header Connection 'upgrade';
// proxy_set_header Host $host;
// proxy_cache_bypass $http_upgrade;
// }
// But you would have better performance with uWS.SSLApp and using a dedicated port for uws
// serverURL: wss://example.com:8080/;
// To generate a self-signed certificate for local development, you can use
// npx webpack serve --server-type https
// and stop it with ctrl+c, it will generate the file node_modules/.cache/webpack-dev-server/server.pem
// Replace the self-signed certificate with a letsencrypt one in production.
const app = uWS.SSLApp({
key_file_name: "node_modules/.cache/webpack-dev-server/server.pem",
cert_file_name: "node_modules/.cache/webpack-dev-server/server.pem"
});
// const app = uWS.App();
// Get port or default to 8080
const port = process.env.PORT || 8080;
// Threshold for instancing a room
const maxOccupantsInRoom = 100;
// Store for rooms and connections
const rooms = new Map();
const sockets = new Map();
const encode = (event, data) => JSON.stringify({ event, data });
// MIME types for static file serving
const MIME_TYPES = {
".html": "text/html",
".js": "application/javascript",
".css": "text/css",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
".wav": "audio/wav",
".mp3": "audio/mpeg",
".mp4": "video/mp4",
".woff": "application/font-woff",
".ttf": "application/font-ttf",
".eot": "application/vnd.ms-fontobject",
".otf": "application/font-otf",
".wasm": "application/wasm",
".glb": "model/gltf-binary"
};
const tmpArray = new Uint32Array(1);
function generateUniqueId() {
return String(crypto.getRandomValues(tmpArray)[0]);
}
// Static file handler
function serveStatic(res, reqPath) {
let filepath = path.join(__dirname, "..", "examples", reqPath);
// Serve index.html for directory requests
if (!path.extname(filepath)) {
filepath = path.join(filepath, "index.html");
}
try {
const stat = fs.statSync(filepath);
if (!stat.isFile()) {
res.writeStatus("404 Not Found").end();
return;
}
const ext = path.extname(filepath);
const contentType = MIME_TYPES[ext] || "application/octet-stream";
const content = fs.readFileSync(filepath);
res.writeHeader("Content-Type", contentType);
res.end(content);
} catch (e) {
res.writeStatus("404 Not Found").end();
}
}
// Handle HTTP requests
app.any("/*", (res, req) => {
const url = req.getUrl();
serveStatic(res, url);
});
// WebSocket handling
app.ws("/*", {
idleTimeout: 60,
maxPayloadLength: 16 * 1024 * 1024,
compression: uWS.SHARED_COMPRESSOR,
sendPingsAutomatically: true, // that's the default
open(ws) {
const socketId = generateUniqueId();
sockets.set(socketId, ws);
ws.socketId = socketId;
ws.subscribe(socketId);
console.log("user connected", socketId);
},
message(ws, message) {
const msg = JSON.parse(Buffer.from(message).toString());
switch (msg.event) {
case "joinRoom": {
const { room } = msg.data;
let curRoom = room;
let roomInfo = rooms.get(room);
if (!roomInfo) {
roomInfo = {
name: room,
occupants: {},
occupantsCount: 0
};
rooms.set(room, roomInfo);
}
if (roomInfo.occupantsCount >= maxOccupantsInRoom) {
const roomPrefix = `${room}--`;
let availableRoomFound = false;
let numberOfInstances = 1;
for (const [roomName, roomData] of rooms.entries()) {
if (roomName.startsWith(roomPrefix)) {
numberOfInstances++;
if (roomData.occupantsCount < maxOccupantsInRoom) {
availableRoomFound = true;
curRoom = roomName;
roomInfo = roomData;
break;
}
}
}
if (!availableRoomFound) {
const newRoomNumber = numberOfInstances + 1;
curRoom = `${roomPrefix}${newRoomNumber}`;
roomInfo = {
name: curRoom,
occupants: {},
occupantsCount: 0
};
rooms.set(curRoom, roomInfo);
}
}
const joinedTime = Date.now();
roomInfo.occupants[ws.socketId] = joinedTime;
roomInfo.occupantsCount++;
ws.curRoom = curRoom;
ws.subscribe(curRoom);
ws.send(encode("connectSuccess", { joinedTime, socketId: ws.socketId }));
app.publish(
curRoom,
encode("occupantsChanged", {
occupants: roomInfo.occupants
})
);
break;
}
case "send": {
const { to, ...data } = msg.data;
app.publish(to, encode("send", data));
break;
}
case "broadcast": {
if (ws.curRoom) {
app.publish(ws.curRoom, encode("broadcast", msg.data));
}
break;
}
}
},
close(ws) {
const roomInfo = rooms.get(ws.curRoom);
if (roomInfo) {
console.log("user disconnected", ws.socketId);
delete roomInfo.occupants[ws.socketId];
roomInfo.occupantsCount--;
app.publish(
ws.curRoom,
encode("occupantsChanged", {
occupants: roomInfo.occupants
})
);
if (roomInfo.occupantsCount === 0) {
console.log("everybody left room");
rooms.delete(ws.curRoom);
}
}
sockets.delete(ws.socketId);
}
});
app.listen(port, (token) => {
if (token) {
console.log(`Listening on https://localhost:${port}`);
} else {
console.log(`Failed to listen on port ${port}`);
}
});