always use both servers

This commit is contained in:
Sebastian 2023-03-15 07:23:20 -03:00
parent 41339c1db2
commit 0700bbe9d1
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069

View File

@ -31,7 +31,6 @@ export async function serve(opts: {
port: number;
source?: string;
development?: boolean;
insecure?: boolean;
examplesLocationJs?: string;
examplesLocationCss?: string;
onUpdate?: () => Promise<void>;
@ -39,10 +38,10 @@ export async function serve(opts: {
const app = express();
app.use(PATHS.APP, express.static(opts.folder));
const server = opts.insecure
? http.createServer(app)
: https.createServer(httpServerOptions, app);
logger.info(`serving ${opts.folder} on ${opts.port}`);
const servers = [
http.createServer(app),
https.createServer(httpServerOptions, app),
];
logger.info(` ${PATHS.APP}: application`);
logger.info(` ${PATHS.EXAMPLE}: examples`);
logger.info(` ${PATHS.WS}: websocket`);
@ -55,15 +54,17 @@ export async function serve(opts: {
ws.send("welcome");
});
server.on("upgrade", function upgrade(request, socket, head) {
const { pathname } = parse(request.url || "");
if (pathname === PATHS.WS) {
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit("connection", ws, request);
});
} else {
socket.destroy();
}
servers.forEach(function addWSHandler(server) {
server.on("upgrade", function upgrade(request, socket, head) {
const { pathname } = parse(request.url || "");
if (pathname === PATHS.WS) {
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit("connection", ws, request);
});
} else {
socket.destroy();
}
});
});
const sendToAllClients = function (data: object): void {
@ -121,6 +122,9 @@ export async function serve(opts: {
res.send("ok");
});
server.listen(opts.port);
servers.forEach(function startServer(server, index) {
logger.info(`serving ${opts.folder} on ${opts.port + index}`);
server.listen(opts.port + index);
});
}
}