-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.js
211 lines (161 loc) · 5.47 KB
/
server1.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
//DB
const mongoose = require('mongoose')
const validator = require('validator')
mongoose.connect('mongodb://127.0.0.1:27017/chat-application', {
useNewUrlParser: true,
useCreateIndex: true
});
const ActiveFriend = mongoose.model('ActiveFriend', {
uniqueId: {
type: String,
trim: true
},
userName: {
type: String,
trim: true
}
});
const MessageUser = mongoose.model('MessageUser', {
uniqueId: {
type: String,
trim: true
},
userName: {
type: String,
trim: true
},
message: {
type: String,
trim: true
}
});
app.use(express.json())
app.get('/messageUser', (req, res) => {
MessageUser.find({}).then((messages) => {
res.send(messages);
}).catch((e) => {
console.log(e);
});
});
app.get('/', (req, res) => {
res.send('Chat Server is running on port 3000')
});
const users = {}
io.on('connection', (socket) => {
console.log('-------------------User connected')
socket.on('activeList', function (userNickname) {
console.log(userNickname);
// users[socket.id] = userNickname
ActiveFriend.find({}).then((messages) => {
// res.send(messages);
for (var i = 0; i < messages.length; i++) {
var obj = (messages[i]);
var senderNickname = obj.userName;
// var messageContent = obj.message;
console.log(senderNickname + " ase ");
let message = { "senderNickname": senderNickname }
// send the message to the client side
io.to(`${socket.id}`).emit('listofActive', message);
// io.emit('message', message);
}
}).catch((e) => {
console.log(e);
});
});
socket.on('join', function(userNickname) {
users[socket.id] = userNickname
console.log("users[socket.id] = userNickname 94 = " + users[socket.id])
const active_list = new ActiveFriend({
uniqueId: socket.id,
userName: userNickname
});
active_list.save().then(() => {
console.log(active_list)
}).catch((error) => {
console.log('Error!', error)
});
const messageName = new MessageUser({
uniqueId: userNickname,
userName: userNickname,
message: "*$*"
});
messageName.save().then(() => {
console.log(messageName)
}).catch((error) => {
console.log('Error!', error)
});
console.log(userNickname +" : has joined the chat " );
let message = { "senderNickname": userNickname }
// socket.broadcast.emit('userjoinedthechat',message);
socket.broadcast.emit('userjoinedthechat',message);
});
socket.on('previousMessage', function (userNickname) {
MessageUser.find({}).then((messages) => {
// res.send(messages);
for(var i=0;i<messages.length;i++){
var obj = (messages[i]);
var senderNickname = obj.userName;
var messageContent = obj.message;
let message = { "message": messageContent, "senderNickname": senderNickname }
// send the message to the client side
io.to(`${socket.id}`).emit('message',message);
// io.emit('message', message);
}
}).catch((e) => {
console.log(e);
});
});
socket.on('messagedetection', (senderNickname,messageContent) => {
const messageName = new MessageUser({
uniqueId: senderNickname,
userName: senderNickname,
message: messageContent
});
messageName.save().then(() => {
console.log(messageName)
}).catch((error) => {
console.log('Error!', error)
});
//log the message in console
console.log(senderNickname+" :" +messageContent)
//create a message object
let message = {"message":messageContent, "senderNickname":senderNickname}
// send the message to the client side
io.emit('message', message );
});
socket.on('disconnect', function () {
// socket.broadcast.emit('userdisconnect', users[socket.id])
var userNickname = users[socket.id]
ActiveFriend.deleteOne({
uniqueId : socket.id
}).then ( (result) => {
console.log(result)
}).catch((e)=>{
console.log(e)
});
const messageName = new MessageUser({
uniqueId: userNickname,
userName: userNickname,
message: "$*$"
});
messageName.save().then(() => {
console.log(messageName)
}).catch((error) => {
console.log('Error!', error)
});
delete users[socket.id]
console.log(userNickname+ ' disconnected ')
let message = { "senderNickname": userNickname }
// socket.broadcast.emit('userjoinedthechat',message);
socket.broadcast.emit('userdisconnect', message);
// socket.broadcast.emit("userdisconnect"," user has left ")
});
});
server.listen(3000,()=>{
console.log('Message: Node app is running on port 3000');
});