36 lines
901 B
JavaScript
36 lines
901 B
JavaScript
const express = require("express");
|
|
const app = express();
|
|
const http = require("http");
|
|
const server = http.createServer(app);
|
|
|
|
const { Server } = require("socket.io");
|
|
const io = new Server(server, {
|
|
cors: {
|
|
origin: "http://192.168.0.131:3000", // Replace with the origin of your client application
|
|
methods: ["GET", "POST", "WS"], // Allowed HTTP methods
|
|
}
|
|
});
|
|
|
|
app.get("/", (req, res) => {
|
|
// res.sendFile(__dirname + "/index.html");
|
|
res.send(`Upgrade`);
|
|
});
|
|
|
|
io.on("connection", (socket) => {
|
|
console.log("A user connected");
|
|
|
|
// Handle custom events from the client
|
|
socket.on("chat message", (msg) => {
|
|
io.emit("chat message", msg); // Emit the message to all connected clients
|
|
});
|
|
|
|
// Handle disconnections
|
|
socket.on("disconnect", () => {
|
|
console.log("User disconnected");
|
|
});
|
|
});
|
|
|
|
server.listen(4000, () => {
|
|
console.log("Listening on port 4000");
|
|
});
|