-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoglight.js
More file actions
144 lines (123 loc) · 3.5 KB
/
Loglight.js
File metadata and controls
144 lines (123 loc) · 3.5 KB
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
const process = require("process");
const path = require("path");
const fs = require("fs");
const url = require("url");
const http = require("http");
const portArgIndex = process.argv.indexOf("-p");
let PORT;
let DEFAULT_PORT = 8090;
if (portArgIndex !== -1 && process.argv.length > portArgIndex + 1) {
const portString = process.argv[portArgIndex + 1];
const port = parseInt(portString, 10);
if (isNaN(port)) {
PORT = DEFAULT_PORT; // Default port
} else {
PORT = port;
}
} else {
PORT = DEFAULT_PORT; // Default port
}
const { WebSocketServer } = require("ws");
const server = http.createServer();
const wsServer = new WebSocketServer({ server });
const LOGGER = "logger";
const connections = {};
const devTools = {};
const broadcast = () => {
try {
const message = JSON.stringify(devTools);
const app = Object.keys(devTools).find((key) => key !== LOGGER) || "application";
const msgJson = JSON.parse(message);
if (JSON.stringify(msgJson[app]?.state)) {
const msg = JSON.stringify(msgJson[app].state);
connections[LOGGER].send(msg);
}
} catch (e) {
console.log(e);
}
};
const handleMessage = (bytes, appName) => {
try {
const message = JSON.parse(bytes.toString());
const logger = devTools[appName];
logger.state = message;
broadcast();
} catch (e) {
console.log(e);
}
};
const handleClose = (appName) => {
try {
delete connections[appName];
delete devTools[appName];
broadcast();
} catch (e) {
console.log(e);
}
};
const updateApplicationConnected = () => {
try {
const appNames = { connection: Object.keys(devTools) };
connections[LOGGER].send(JSON.stringify(appNames));
} catch (e) {
console.log(e);
}
};
wsServer.on("connection", (connection, request) => {
const { appName } = url.parse(request.url, true).query;
console.log(`${appName} connected`);
connections[appName] = connection;
devTools[appName] = {
appName,
state: {},
};
updateApplicationConnected();
console.log("===>", Object.keys(devTools));
connection.on("message", (message) => handleMessage(message, appName));
connection.on("close", () => handleClose(appName));
});
server.on("request", (req, res) => {
const pathname = url.parse(req.url).pathname;
// Serve the index.html for root path (/)
if (pathname === "/" || pathname === "") {
const filePath = path.join(__dirname, `public/index.html`);
fs.readFile(filePath, (err, data) => {
if (err) {
res.statusCode = 404;
res.end("Not found");
} else {
res.setHeader("Content-Type", "text/html");
res.end(data);
}
});
} else {
// Serve static files from the public folder
const filePath = path.join(__dirname, "public", pathname);
const extname = path.extname(filePath);
fs.readFile(filePath, (err, data) => {
if (err) {
res.statusCode = 404;
res.end("Not found");
} else {
let contentType = "text/plain"; // Default content type
switch (extname) {
case ".html":
contentType = "text/html";
break;
case ".css":
contentType = "text/css";
break;
case ".js":
contentType = "text/javascript";
break;
}
res.setHeader("Content-Type", contentType);
res.end(data);
}
});
}
});
server.listen(PORT, () => {
console.log(`WebSocket Server is running on port ${PORT}`);
console.log(`Open the logger: http://localhost:${PORT}`);
});