-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (49 loc) · 1.61 KB
/
index.js
File metadata and controls
66 lines (49 loc) · 1.61 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// const express = require('express');
// const http = require('http');
// const socketIo = require('socket.io');
import express from 'express';
import {Server as httpserver} from 'http'
import {Server as socket} from 'socket.io';
// Initialize express app and create an HTTP server
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const dirname1 = dirname(fileURLToPath(import.meta.url));
const app = express();
const server = new httpserver(app);
const io = new socket(server, {
cors: {
origin: "*", // Allow all origins for now; restrict later for security
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type"],
credentials: true
}
});
const users = {};
// app.use(express.static("public" , ""))
// Serve the static files (e.g., client.js, HTML, CSS)
// app.use(express.static('public'));
app.use("/public", express.static("public"))
app.get("/" , (req,res)=>{
res.sendFile(dirname1+"/index.html")
})
// Socket.io logic for handling connections
io.on('connection', socket => {
socket.on('new-user-joined', name => {
console.log('User is connected');
users[socket.id] = name;
socket.broadcast.emit('user-joined', name);
});
socket.on('send', message => {
socket.broadcast.emit('recive', { message: message, name: users[socket.id] });
});
socket.on('sendImage', (Image) => {
socket.broadcast.emit('receiveImage', { Image, name: users[socket.id] });
});
socket.on('disconnect', () => {
socket.broadcast.emit('leave', users[socket.id]);
delete users[socket.id];
});
});
server.listen( 3000, () => {
console.log("Server is running");
});