-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
25 lines (21 loc) · 943 Bytes
/
app.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
require("dotenv").config({ path: __dirname + "/config/.env" });
const WebSocketServer = require("ws").WebSocketServer;
const WebSocketConnectionStateHandler = require("./src/WebSocketConnectionStateHandler.js");
const { clients, jobManager, messageHandler } = require("./instances.js");
const http = require("http");
const app = require("./src/HttpServer.js");
const PORT = process.env.NODE_ENV === "production" ? 22636 : 8080;
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
const connectionStateHandler = new WebSocketConnectionStateHandler(
clients,
jobManager
);
wss.on("connection", (ws, req) => {
connectionStateHandler.handleNewConnection(ws, req);
ws.on("message", (message) => messageHandler.handleMessage(ws, message));
ws.on("close", () => connectionStateHandler.handleConnectionClose(ws));
});
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}!`);
});