54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
|
import { createServer } from "node:http";
|
||
|
import next from "next";
|
||
|
import { Server } from "socket.io";
|
||
|
import {fork} from "node:child_process"
|
||
|
|
||
|
const dev = process.env.NODE_ENV !== "production";
|
||
|
const hostname = "0.0.0.0";
|
||
|
const port = 3000;
|
||
|
// when using middleware `hostname` and `port` must be provided below
|
||
|
const app = next({ dev, hostname, port });
|
||
|
const handler = app.getRequestHandler();
|
||
|
|
||
|
app.prepare().then(() => {
|
||
|
const httpServer = createServer(handler);
|
||
|
|
||
|
const io = new Server(httpServer);
|
||
|
|
||
|
|
||
|
io.on("connection", (socket) => {
|
||
|
console.log("connected")
|
||
|
socket.emit("msg","connected")
|
||
|
|
||
|
io.emit("msg","io.emit");
|
||
|
});
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
// socket_global.emit("msg","socket_global")
|
||
|
|
||
|
httpServer
|
||
|
.once("error", (err) => {
|
||
|
console.error(err);
|
||
|
process.exit(1);
|
||
|
})
|
||
|
.listen(port, () => {
|
||
|
console.log(`> Ready on http://${hostname}:${port}`);
|
||
|
});
|
||
|
|
||
|
|
||
|
const forked = fork("./src/lib/scanner.js")
|
||
|
|
||
|
forked.on("message", msg => {
|
||
|
console.log("received",msg)
|
||
|
io.emit("msg",msg);
|
||
|
|
||
|
})
|
||
|
|
||
|
forked.send({ hello: "world" })
|
||
|
|
||
|
|
||
|
});
|