-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (52 loc) · 1.52 KB
/
app.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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Define a mapping of special words and their corresponding emojis
const emojiMapping = {
'hey': '👋',
'smile': '😊',
'heart': '❤️',
'love': '❤️',
'react': '⚛️',
'woah': '😲',
'lol': '😂',
'like': '❤️',
'congratulations': '🎉',
// Add more mappings as needed
};
const onlineUsers = [];
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('user join', (username) => {
onlineUsers.push(username);
io.emit('user join', onlineUsers);
socket.emit('online users', onlineUsers);
});
socket.on('chat message', (msg) => {
// Replace special words with emojis
const words = msg.text.split(' ');
for (let i = 0; i < words.length; i++) {
if (emojiMapping.hasOwnProperty(words[i].toLowerCase())) {
words[i] = emojiMapping[words[i].toLowerCase()];
}
}
msg.text = words.join(' ');
io.emit('chat message', msg);
});
socket.on('disconnect', () => {
const index = onlineUsers.indexOf(socket.username);
if (index !== -1) {
onlineUsers.splice(index, 1);
io.emit('user leave', onlineUsers);
}
console.log('A user disconnected');
});
});
http.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});