-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
147 lines (120 loc) · 3.8 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
var express=require('express'),
app=express(),
server=require('http').createServer(app),
io=require('socket.io').listen(server);
usernames=[];
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'node_db'
});
connection.connect((err)=>{
if(err){
throw err;
}
console.log('Mysql Connected....');
});
//=======create database==========================
app.get('/createdb',(req,res)=>{
let sql='CREATE DATABASE node_db';
connection.query(sql,(err, result) =>{
if(err)throw err;
console.log(result);
res.send('Database Created.....');
});
});
//=======create row==========================
app.get('/createusertable',(req,res)=>{
let sql='CREATE TABLE users(id int AUTO_INCREMENT, username VARCHAR(255), password VARCHAR(255), PRIMARY KEY(id), UNIQUE KEY(username))';
connection.query(sql,(err, result) =>{
if(err)throw err;
console.log(result);
res.send('.......User Table Created.....');
});
});
//==================adding new user===================================
app.get('/addingnewuser',(req,res)=>{
let newuser={username:'gilbert', password:'niyongere'};
let sql='INSERT INTO users SET ?';
connection.query(sql,newuser,(err, result) =>{
if(err)throw err;
console.log(result);
res.send('New User added.....');
});
});
//=====================get users===========================================
app.get('/getusers',(req,res)=>{
let sql='SELECT * FROM users';
connection.query(sql, (err, results) =>{
if(err)throw err;
console.log(results);
res.send('Users Selected....');
});
});
//===================================select single user=================================================
app.get('/getuser/:id',(req,res)=>{
let sql =`SELECT * FROM users WHERE id = ${req.params.id}`;
connection.query(sql, (err, result) =>{
if(err)throw err;
console.log(result);
res.send('User Selected....');
});
});
//==========================================================================
server.listen(process.env.PORT || 3000);
console.log('Server Running..');
app.get('/',function(req,res){
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection',function(socket){
console.log('Socket Connected..');
socket.on('nuser', function(data1, data2, callback){
if(usernames.indexOf(data1)!=-1){
callback(false);
}else{
callback(true);
socket.username = data1;
socket.password = data2;
console.log(socket.password);
console.log(socket.username);
let newuser={username:socket.username, password:socket.password};
let sql='INSERT INTO users SET ?';
connection.query(sql,newuser,(err, result) =>{
if(err)throw err;
console.log(result);
//res.send('New User added.....');
});
//=====================================================
let signedUser = socket.username;
let sql1 = `SELECT * FROM users WHERE username = '${signedUser}'`;
connection.query(sql1, (err, result) =>{
if(err)throw err;
console.log('---------Checking signed user----------');
console.log(result);
console.log(result["username"]);
//res.send('Username updated successfully.....');
});
//========================================================
usernames.push(socket.username);
updateUsernames();
}
});
//update username
function updateUsernames(){
io.sockets.emit('usernames',usernames);
}
//send message...
socket.on('smessage', function(data1){
io.sockets.emit('nmessage', {msg: data1, user: socket.username});
});
//===========disconnect =======
socket.on('disconnect', function(data1){
if(!socket.username){
return;
}
usernames.splice(usernames.indexOf(socket.username,1));
updateUsernames();
});
});