-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·82 lines (68 loc) · 2.42 KB
/
index.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
#!/usr/bin/env node
const express = require("express");
const opn = require("opn");
const path = require("path");
const app = express();
const port = 5649;
app.get("/", (req, res) => {
// res.sendFile(__dirname + "/index.html");
res.send(html);
});
// Serve static files from the 'public' folder
app.use("/public", express.static(path.join(__dirname, "public")));
const server = app.listen(port, () => {
console.log(`Server listening on port ${port}`);
console.log("Opening logscren on http://localhost:" + port);
console.log(
"Loving logscreen? Consider supporting the project by sponsoring on GitHub: https://github.com/sponsors/soorajshankar"
);
opn("http://localhost:" + port); // Open the browser
});
// Set up Socket.io for real-time communication
const io = require("socket.io")(server);
// Socket.io connection event
io.on("connection", (socket) => {
// console.log("::Browser connected");
// Send the real-time input to the connected client
process.stdin.on("data", (chunk) => {
// console.log('Received data:___')
const logLine = chunk.toString();
const timestampedLog = `${new Date().toISOString()} :: ${logLine}`;
socket.emit("input", timestampedLog);
});
});
// Pass the io object to the index.html file
app.use((req, res, next) => {
res.locals.io = io;
next();
});
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real-time Input with React</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<style>
.json-container[hidden] {
display: none;
}
.json-container button {
display: block;
margin-bottom: 8px;
}
</style>
</head>
<body class="font-sans bg-gray-100 p-6">
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<!-- Load uuid library from CDN -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/uuidv4.min.js"></script>
<script type="text/babel" src="public/app.jsx"></script>
</body>
</html>
`;