Sockets funcionais

This commit is contained in:
JotaChina committed 2025-11-04 16:33:21 -03:00
1 parent 97c0daf697
commit d38cd9e60f
4 files changed
+89 -21

No files matched your search

+33 -17
View File
@@ -1,14 +1,15 @@
// server.js - VERSÃO 6 (ACK + Clock Sync + Logs Dinâmicos + Persistência Autoritativa)
// server.js - VERSÃO 6 (ACK + Clock Sync + Logs Dinâmicos + Persistência Autoritativa) - HTTPS ENABLED
// =========================
// IMPORTS PRINCIPAIS
// =========================
const express = require("express");
const { createServer } = require("http");
const https = require("https");
const { Server } = require("socket.io");
const fs = require("fs");
const path = require("path");
const pino = require("pino");
//const { PORT_SOCK } = require("../config.js");
// -------------------------
// LOGGER DINÂMICO (GERENCIADOR)
@@ -57,11 +58,29 @@ function getActionLoggerForRoom(roomName) {
}
// -------------------------
// Configuração do Servidor
// Configuração do Servidor (HTTPS)
// -------------------------
const app = express();
const httpServer = createServer(app);
const PORT = process.env.PORT || 33007;
const PORT = process.env.PORT || 33001;
// ====== HTTPS (Opção B) ======
const CERT_FULLCHAIN = process.env.SSL_FULLCHAIN || "/etc/letsencrypt/live/alice.ufsj.edu.br/fullchain.pem";
const CERT_PRIVKEY = process.env.SSL_PRIVKEY || "/etc/letsencrypt/live/alice.ufsj.edu.br/privkey.pem";
if (!fs.existsSync(CERT_FULLCHAIN) || !fs.existsSync(CERT_PRIVKEY)) {
console.error("[HTTPS] Certificados não encontrados.\n" +
` fullchain: ${CERT_FULLCHAIN}\n` +
` privkey : ${CERT_PRIVKEY}\n` +
"Defina SSL_FULLCHAIN/SSL_PRIVKEY ou instale os certificados no caminho padrão.");
process.exit(1);
}
const httpsOptions = {
cert: fs.readFileSync(CERT_FULLCHAIN),
key : fs.readFileSync(CERT_PRIVKEY),
};
const httpsServer = https.createServer(httpsOptions, app);
// -------------------------
// Persistência por sala
@@ -126,17 +145,13 @@ function saveRoom(roomName) {
}
// -------------------------
// Server / IO
// Server / IO
// -------------------------
const io = new Server(httpServer, {
const io = new Server(httpsServer, {
cors: {
origin: [
"https://alice.ufsj.edu.br",
"http://127.0.0.1:33007",
"http://localhost:33007",
"http://localhost:5173",
"http://127.0.0.1:5173",
],
// CORREÇÃO: Simplificado para a origem real do cliente, que é onde o HTML é servido.
// O cliente provavelmente é servido a partir de https://alice.ufsj.edu.br (porta 443).
origin: "https://alice.ufsj.edu.br",
methods: ["GET", "POST"],
credentials: true,
},
@@ -509,6 +524,7 @@ app.get("/", (req, res) => {
);
});
httpServer.listen(PORT, () => {
console.log(`Servidor escutando na porta http://localhost:${PORT}`);
});
// ====== START ======
httpsServer.listen(PORT, () => {
console.log(`Servidor escutando na porta https://localhost:${PORT}`);
});