-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (49 loc) · 1.83 KB
/
server.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
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
67
68
69
70
71
72
73
74
75
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Serve static files
app.use(express.static('public'));
const clients = {};
// Socket.IO logic
io.on('connection', (socket) => {
console.log('A client connected:', socket.id);
socket.on('message', function (data) {
console.log('Message received: ', data);
// Update client data
// Broadcast the message to all clients
// io.emit('message', { id: socket.id, data: data });
io.emit('message', data );
});
socket.send("hello client i am Manoj kumar");
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
// clients = clients.filter(clientId => clientId !== socket.id);
});
});
// Handle HTTP requests
// app.get('/', (req, res) => {
// const randomNumber = Math.floor(Math.random() * 100);
// // Emit a message to all connected clients including the sender
// io.emit('msg', { number: 567 });
// res.status(200).json({ number: 111 });
// });
app.get('/socket.io/?EIO=4', (req, res) => {
res.send('Socket.IO server');
io.on('connection', (socket) => {
console.log('A client connected:', socket.id);
socket.on('message', function (data) {
console.log('Message received: ', data);
io.emit('message',data)
});
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
});
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});