-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (31 loc) · 1.05 KB
/
index.js
File metadata and controls
39 lines (31 loc) · 1.05 KB
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
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const cors = require('cors');
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*", // Allow all origins (you can specify specific origins here)
methods: ["GET", "POST"], // Allowed methods
allowedHeaders: ["my-custom-header"], // Custom headers (if any)
credentials: true // Allow credentials (cookies, authorization headers, etc.)
}
});
app.use(cors());
io.on("connection", (socket) => {
console.log("user connected", socket.id);
socket.on("message", (msg) => {
console.log("Message received: " + msg);
// Broadcast the message to all connected clients
io.emit("message", msg);
});
socket.on("disconnect", () => {
console.log("User disconnected");
});
});
const PORT = process.env.PORT || 4000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
module.exports = app;