-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
92 lines (81 loc) · 2.35 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
const http = require("http");
const app = require("./backend/app");
const debug = require("debug")("node-angular");
const { Server } = require("socket.io");
const sockets = require("./sockets");
// Ensure port is valid number
const normalizePort = (val) => {
let portNumber = parseInt(val, 10);
if (isNaN(portNumber)) {
// named pipe
return val;
}
if (portNumber >= 0) {
// port number
return portNumber;
}
return false;
};
// Check what type of error occurs & exit gracefully
const onError = (error) => {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
};
// Log to Console
const onListening = () => {
const addr = server.address();
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
debug("Listening on " + bind);
};
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
// Use Express app to start Server
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: process.env.ANGULAR_CLIENT_URL,
methods: ["GET", "POST"],
},
});
server.on("error", onError);
server.on("listening", onListening);
io.on("connection", (socket) => {
console.log("User connected " + socket.id);
let user = sockets.onConnect(socket);
socket.on("disconnect", (reason) => {
sockets.onDisconnect(user);
});
socket.on("sendAPIKey", (key) => {
sockets.sendAPIKey(user, key);
});
socket.on("logout", (key) => {
sockets.logoutUser(user);
});
socket.on("viewDocument", (docID) => {
sockets.viewDocument(user, docID);
});
socket.on("endViewingDocument", () => {
sockets.endViewingDocument(user);
});
socket.on("postComment", (comment, line) => {
sockets.postComment(user, comment, line);
});
socket.on("sendChatMessage", (msg) => {
sockets.sendChatMessage(user, msg);
});
});
server.listen(port);