-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
193 lines (169 loc) · 4.87 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
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
// Load required modules
const http = require("http"); // http server core module
const path = require("path");
const express = require("express"); // web framework external module
const socketIo = require("socket.io"); // web socket external module
const easyrtc = require("open-easyrtc"); // EasyRTC external module
// Set process name
process.title = "networked-aframe-server";
// Get port or default to 8080
const port = process.env.PORT || 8080;
// Setup and configure Express http server.
const app = express();
app.use(express.static("public"));
const rooms = [
{
name: "mindfullbliss 1",
maxPlayers: 30,
currentPlayers: 5,
},
{
name: "moonworld 2",
maxPlayers: 30,
currentPlayers: 11,
},
{ name: "artworld 2", maxPlayers: 30, currentPlayers: 11 },
{
name: "loobyworld 2",
maxPlayers: 30,
currentPlayers: 11,
},
{
name: "rip take off world 2",
maxPlayers: 30,
currentPlayers: 11,
},
{ name: "matrixworld 2", maxPlayers: 30, currentPlayers: 11 },
{ name: "bitcoinworld 2", maxPlayers: 30, currentPlayers: 11 },
// Add more rooms as needed
];
// Serve the room files
app.get("/mindfullbliss", (req, res) => {
res.sendFile(path.join(__dirname, "public/mindfullbliss.html"));
});
app.get("/moonworld", (req, res) => {
res.sendFile(path.join(__dirname, "public/moonworld.html"));
});
// Add similar lines for other room files
// Enable CORS
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});
// Add a new endpoint to serve the view count
let viewCount = 3398310;
app.get("/viewcount", (req, res) => {
viewCount++;
const formattedViewCount = viewCount.toLocaleString();
res.send(formattedViewCount);
});
// Start Express http server
const webServer = http.createServer(app);
// Start Socket.io so it attaches itself to Express server
const socketServer = socketIo.listen(webServer, { "log level": 1 });
const myIceServers = [
{ urls: "stun:stun1.l.google.com:19302" },
{ urls: "stun:stun2.l.google.com:19302" },
// {
// "urls":"turn:[ADDRESS]:[PORT]",
// "username":"[USERNAME]",
// "credential":"[CREDENTIAL]"
// },
// {
// "urls":"turn:[ADDRESS]:[PORT][?transport=tcp]",
// "username":"[USERNAME]",
// "credential":"[CREDENTIAL]"
// }
];
easyrtc.setOption("appIceServers", myIceServers);
easyrtc.setOption("logLevel", "debug");
easyrtc.setOption("demosEnable", false);
// Overriding the default easyrtcAuth listener, only so
// we can directly access its callback
easyrtc.events.on(
"easyrtcAuth",
(socket, easyrtcid, msg, socketCallback, callback) => {
easyrtc.events.defaultListeners.easyrtcAuth(
socket,
easyrtcid,
msg,
socketCallback,
(err, connectionObj) => {
if (err || !msg.msgData || !msg.msgData.credential || !connectionObj) {
callback(err, connectionObj);
return;
}
connectionObj.setField("credential", msg.msgData.credential, {
isShared: false,
});
console.log(
"[" + easyrtcid + "] Credential saved!",
connectionObj.getFieldValueSync("credential")
);
callback(err, connectionObj);
}
);
}
);
// To test, let's print the credential to the console for every room join!
easyrtc.events.on(
"roomJoin",
(connectionObj, roomName, roomParameter, callback) => {
console.log(
"[" + connectionObj.getEasyrtcid() + "] Credential retrieved!",
connectionObj.getFieldValueSync("credential")
);
easyrtc.events.defaultListeners.roomJoin(
connectionObj,
roomName,
roomParameter,
callback
);
}
);
// Add chat message listener
easyrtc.events.on(
"emitChatMessage",
(connectionObj, msgData, socketCallback, callback) => {
const roomId = msgData.roomId;
const message = msgData.message;
const senderId = connectionObj.getEasyrtcid();
const chatMessage = {
senderId,
message,
timestamp: new Date(),
};
// Broadcast the message to all clients in the same room
connectionObj
.getApp()
.rooms.getRoom(roomId)
.emitEvent("chatMessage", chatMessage);
callback(null);
}
);
// Start EasyRTC server
easyrtc.listen(app, socketServer, null, (err, rtcRef) => {
console.log("Initiated");
rtcRef.events.on(
"roomCreate",
(appObj, creatorConnectionObj, roomName, roomOptions, callback) => {
console.log("roomCreate fired! Trying to create: " + roomName);
appObj.events.defaultListeners.roomCreate(
appObj,
creatorConnectionObj,
roomName,
roomOptions,
callback
);
}
);
});
// Listen on port
webServer.listen(port, () => {
console.log("listening on http://localhost:" + port);
});