const express = require('express');
const cors = require('cors');
const { createServer } = require('http'); // Ensure this is at the top with other requires
const { Server } = require('socket.io');
const axios = require('axios');
const Redis = require('ioredis');
const userSockets = new Map();
// CONFIG //
require('dotenv').config();
const stripe = Stripe(process.env.STRIPE_SECRET_KEY);
const app = express();
const corsOptions = {
origin: 'https://dev-noisse.vercel.app', // Replace with your frontend domain
methods: ['GET', 'POST'], // Specify the allowed HTTP methods
allowedHeaders: ['Content-Type'], // Specify the allowed headers
credentials: true // Allow sending cookies
};
app.use(cors(corsOptions));
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "https://dev-noisse.vercel.app",
methods: ["GET", "POST"],
},
pingTimeout: 60000, // Increase the ping timeout to 60 seconds
pingInterval: 25000 // Send a ping every 25 seconds
});
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('authenticate', (hunter_id) => {
// Validate hunter_id here if necessary
userSockets.set(hunter_id, socket.id);
});
socket.on('disconnect', () => {
for (const [hunter_id, socketId] of userSockets.entries()) {
if (socketId === socket.id) {
userSockets.delete(hunter_id);
console.log(`User with hunter_id ${hunter_id} disconnected`);
break;
}
}
});
});
......
const PORT = process.env.PORT || 3001;
httpServer.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
io.attach(httpServer);
});
]