-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (170 loc) · 6.1 KB
/
index.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
var express = require('express');
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit: 1000000000,
host: 'localhost',
user: 'root',
password: 'root',
database: 'im',
});
pool.getConnection(function(err){
if(err) {
console.log("Error in DB connection : "+err);
} else {
console.log("connect database ... nn");
}
});
app.use(express.static(__dirname+ '/public'));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
var chatbox = io.of('/chatbox');
chatbox.on('connection', function(socket){
var MEMBER = "";
var memberNo = 0;
var newMem = null;
var a = '';
var rooms = [];
socket.on("person1", function(data) {
MEMBER = data.person;
pool.query("SELECT * FROM members WHERE username = '"+MEMBER+"'", function (err, rows) {
if (err) { console.log("Failed"+err); return; }
if (rows.length <= 0) {
var member= {'username':MEMBER};
pool.query("INSERT INTO members SET ?", member, function(err, rows){
if (err) { console.log("failed to register member"+err); return;}
console.log("registered member succefully "+MEMBER);
});
}else{
console.log("User "+MEMBER+" is already a member");
}
// newMem = setInterval(function () {
pool.query("SELECT * FROM members WHERE username != '"+MEMBER+"' AND id > '"+ memberNo +"' ", function (err, rows) {
if (err) {
console.log("Failed to get members"+err);
return;
}
if (rows.length>0) {
for (var i = 0; i < rows.length; i++) {
memberNo = rows[i].id;
}
socket.emit("members", {"members": rows});
pool.query("SELECT * FROM messages WHERE (reciever = '"+MEMBER+"' AND recieved = 'yes') OR (sender = '"+MEMBER+"' AND recieved = 'yes') ORDER BY id DESC", function (err, rows) {
console.log("going low man");
if (err) {
console.log("Failed to get messages"+err);
return;
}
if (rows.length>0) {
socket.emit("oldmessage", {"message": rows});
}
onConnect(MEMBER);
});
}
});
// }, 100);
});
});
function onConnect(name){
console.log("Mem is"+name);
pool.query("SELECT * FROM chatrooms WHERE (user1 = '"+name+"') OR (user2 = '"+name+"')", function (err, rows) {
if (err) { console.log("Failed"); return; }
if (rows.length <= 0) {
// var room = {'user1':MEMBER, 'user2':friend, 'roomname':roomname};
// pool.query("INSERT INTO chatrooms SET ?", room, function(err, rows){
// if (err) { console.log("failed to register chatroom "+err); return;}
// console.log("registered chatroom succefully "+roomname);
// });
console.log("no friends");
}else{
// console.log("chatroom "+rows[0].roomname+" already exists");
for (var i = 0; i <rows.length; i++) {
if(rows[i].user1==name){
socket.join(rows[i].roomname);
var roomsuck = {"roomname":rows[i].roomname, "roommate":rows[i].user2};
socket.emit("chatroom", roomsuck);
}else{
socket.join(rows[i].roomname);
var roomsuck = {"roomname":rows[i].roomname, "roommate":rows[i].user1};
socket.emit("chatroom", roomsuck);
}
}
}
});
}
socket.on('typing', function (data) {
socket.broadcast.to(data.room).emit("typing", {"from": MEMBER});
});
socket.on("sendMessage",function (data) {
var message = {"sender":MEMBER,"reciever":data.reciever, "message":data.message, "sent": "yes", "type":data.type};
pool.query("INSERT INTO messages SET ?", message, function(err, rows){
if (err) { console.log("failed to send message"+err); return;}
socket.emit("send_success");
});
});
socket.on('messageOwner', function(data) {
var newMessage = setInterval(function() {
var me = data.theowner;
pool.query("SELECT * FROM messages WHERE (reciever = '"+MEMBER+"' AND recieved = 'no')", function (err, rows) {
if (err) {
console.log("Failed to get messages"+err);
return;
}
if (rows.length>0) {
if (socket.emit("message", {"message": rows})) {
pool.query("UPDATE messages SET recieved = 'yes' WHERE (reciever = '"+MEMBER+"') AND recieved = 'no'", function (err, rows) {
});
}
}
});
}, 100);
});
socket.on('oldMesages', function (data) {
var owner=data.theowner;
pool.query("SELECT * FROM messages WHERE (reciever = '"+MEMBER+"' AND sender = '"+owner+"' AND recieved = 'yes') OR (reciever = '"+owner+"' AND sender = '"+MEMBER+"' AND recieved = 'yes') ORDER BY id DESC", function (err, rows) {
if (err) {
console.log("Failed to get messages"+err);
return;
}
if (rows.length>0) {
socket.emit("oldmessage", {"message": rows})
}
});
});
socket.on('disconnect', function () {
socket.broadcast.emit("IsNotActive", {'person':MEMBER});
clearInterval(newMem);
clearInterval(a);
console.log("user discconected");
});
});
http.listen(1234, function() {
console.log("listening on *:1234")
});
// pool.query("SELECT * FROM members WHERE id > '"+ memberNo +"' AND username != '"+MEMBER+"'" , function (err, rows) {
// if (err) {
// console.log("Failed to get new members"+err);
// return;
// }
// if (rows.length>0) {
// for (var i = 0; i < rows.length; i++) {
// memberNo = rows[i].id;
// }
// socket.emit("members", {"members": rows});
// }
// });
// setInterval(function() {
// var roomlength = io.nsps["/chatbox"].adapter.rooms[rows[0].roomname];
// if (roomlength==undefined) {
// return;
// }
// else if (roomlength.length==1){
// socket.emit("isNotActive");
// }
// else{
// socket.emit("isActive");
// }
// }, 1000);