Update chat.js
Browse files
chat.js
CHANGED
@@ -1,36 +1,32 @@
|
|
1 |
const express = require('express');
|
2 |
-
const app = express();
|
3 |
const http = require('http');
|
4 |
-
const
|
5 |
-
const path = require('path');
|
6 |
-
const { Server } = require('socket.io');
|
7 |
-
const io = new Server(server);
|
8 |
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
});
|
14 |
|
|
|
15 |
io.on('connection', (socket) => {
|
16 |
-
console.log(
|
17 |
-
|
18 |
-
|
19 |
-
socket.broadcast.emit('finish typing', socket.handshake.query.name);
|
20 |
-
console.log(socket.handshake.query.name + " disconnected!");
|
21 |
-
io.emit('disconnection notification', socket.handshake.query.name);
|
22 |
-
});
|
23 |
socket.on('chat message', (msg) => {
|
24 |
-
|
25 |
-
});
|
26 |
-
socket.on('typing', () => {
|
27 |
-
socket.broadcast.emit('typing', socket.handshake.query.name);
|
28 |
});
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
});
|
32 |
});
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
});
|
|
|
1 |
const express = require('express');
|
|
|
2 |
const http = require('http');
|
3 |
+
const socketIo = require('socket.io');
|
|
|
|
|
|
|
4 |
|
5 |
+
// Create an Express application
|
6 |
+
const app = express();
|
7 |
+
const server = http.createServer(app);
|
8 |
+
const io = socketIo(server);
|
9 |
|
10 |
+
// Serve static files from the "public" directory
|
11 |
+
app.use(express.static('public'));
|
|
|
12 |
|
13 |
+
// Listen for incoming connections
|
14 |
io.on('connection', (socket) => {
|
15 |
+
console.log('A user connected');
|
16 |
+
|
17 |
+
// Listen for chat messages
|
|
|
|
|
|
|
|
|
18 |
socket.on('chat message', (msg) => {
|
19 |
+
io.emit('chat message', msg); // Broadcast the message to all clients
|
|
|
|
|
|
|
20 |
});
|
21 |
+
|
22 |
+
// Handle disconnection
|
23 |
+
socket.on('disconnect', () => {
|
24 |
+
console.log('A user disconnected');
|
25 |
});
|
26 |
});
|
27 |
|
28 |
+
// Start the server
|
29 |
+
const PORT = process.env.PORT || 7860;
|
30 |
+
server.listen(PORT, () => {
|
31 |
+
console.log(`Server is running on http://localhost:${PORT}`);
|
32 |
});
|