-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (29 loc) · 875 Bytes
/
index.js
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
const express = require('express');
const { Server } = require('socket.io');
const path = require('path');
const app = express();
const port = process.env.PORT || 4000;
// Serve static files from the public directory
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const server = app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
io.on("connection", (socket) => {
console.log("socket connection is ok " + socket.id);
socket.on("chat", (data) => {
io.sockets.emit("chat", data);
});
socket.on("typing", (name) => {
console.log(name + " is typing...");
socket.broadcast.emit("typing", name);
});
});