-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
168 lines (146 loc) · 4.62 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const path = require('path');
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const {formatMessage,
formatMessageI} = require('./utils/messages');
const {
userJoin,
userJoinG,
getCurrentUser,
getCurrentUserG,
getCurrentUserP,
userLeave,
userLeaveG,
getRoomUsers,
getRoomUsersG,
} = require('./utils/users');
var pri=''
var pri2=''
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.get('/private', (req, res)=>{
app.render('/private')
})
// Set static folder
app.use(express.static(path.join(__dirname, 'public')));
const botName = 'Talk Buddy';
const globalRoom = 'global'
// Run when client connects
io.on('connection', socket => {
socket.on('joinRoom', ({ username, room }) => {
const user = userJoin(socket.id, username, room);
const userg = userJoinG(socket.id, username, globalRoom);
socket.join(user.room);
socket.join(userg.room);
// Welcome current user
socket.emit('message', formatMessage(botName, 'Welcome to Talk Buddy!'));
socket.emit('messageG', formatMessage(botName, 'Welcome to Talk Buddy!'));
// Broadcast when a user connects
socket.broadcast
.to(user.room)
.emit(
'message',
formatMessage(botName, `${user.username} has joined the chat`)
);
socket.broadcast
.to(userg.room)
.emit(
'messageG',
formatMessage(botName, `${userg.username} has joined the chat`)
);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
io.to(userg.room).emit('roomUsersG', {
users: getRoomUsersG(userg.room)
});
});
socket.on('priInitiate', userName => {
const user = getCurrentUser(socket.id);
const userP = getCurrentUserP(userName);
const msg = "Private Chat initiated"
console.log(userP)
if(!userP){
socket.emit("wrongUser", "No such user exist")
}
else{
pri=userP.id
pri2=socket.id
io.to(userP.id).emit('messageIni', formatMessage(user.username, msg));
socket.emit('messageIni', formatMessage(userP.username, msg));
}
});
// Listen for chatMessage
socket.on('chatMessage', msg => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.username, msg));
});
socket.on('chatMessageG', msg => {
const user = getCurrentUserG(socket.id);
io.to(user.room).emit('messageG', formatMessage(user.username, msg));
});
socket.on('chatMessageI', msg => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('messageI', formatMessage(user.username, msg));
});
socket.on('chatMessageGI', msg => {
const user = getCurrentUserG(socket.id);
io.to(user.room).emit('messageGI', formatMessage(user.username, msg));
});
socket.on('chatMessageP', msg => {
console.log(pri, msg)
const user = getCurrentUserG(socket.id);
if(user.id === pri){
io.to(pri2).emit('messageP', formatMessage(user.username, msg));
io.to(user.id).emit('messageP', formatMessage(user.username, msg));
}
else if(user.id === pri2){
io.to(pri).emit('messageP', formatMessage(user.username, msg));
io.to(user.id).emit('messageP', formatMessage(user.username, msg));
}
});
socket.on('chatMessagePI',msg => {
const user = getCurrentUserG(socket.id);
if(user.id === pri){
io.to(pri2).emit('messagePI', formatMessage(user.username, msg));
io.to(user.id).emit('messagePI', formatMessage(user.username, msg));
}
else if(user.id === pri2){
io.to(pri).emit('messagePI', formatMessage(user.username, msg));
io.to(user.id).emit('messagePI', formatMessage(user.username, msg));
}
});
// Runs when client disconnects
socket.on('disconnect', () => {
const user = userLeave(socket.id);
const userG = userLeaveG(socket.id);
if (user) {
io.to(user.room).emit(
'message',
formatMessage(botName, `${user.username} has left the chat`)
);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
if (userG) {
io.to(userG.room).emit(
'messageG',
formatMessage(botName, `${userG.username} has left the chat`)
);
// Send users and room info
io.to(userG.room).emit('roomUsersG', {
room: userG.room,
users: getRoomUsersG(userG.room)
});
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));