-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
119 lines (92 loc) · 3.49 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
let express = require("express");
let socket = require("socket.io");
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/chat";
const client = new MongoClient(url, { useUnifiedTopology: true });
MongoClient.connect(url, { useUnifiedTopology: true },function(err, db) { //here db is the client obj
if (err) throw err;
db.auth('chat_admin', 'chat_password')
let dbase = db.db("chat"); //here
let allCollections = [];
dbase.listCollections().toArray(function(err, collections) {
if(err) console.log(err);
//iterate to each collection detail and push just name in array
collections.forEach(eachCollectionDetails => {
allCollections.push(eachCollectionDetails.name);
});
//close client
// client.close();
});
console.log(allCollections);
// if (!allCollections.includes("chats")) {
//
// dbase.createCollection("chats", function (err, res) {
// if (err) throw err;
// console.log("Chats database created!");
// db.close(); //close method has also been moved to client obj
// });
// }else{
// console.log("Chats database found.");
// }
});
let app = express();
app.use(express.static("public"));
let server = app.listen(3456, function() {
console.log("Listening to port 3456.");
});
let io = socket(server);
// Global variables to hold all usernames and rooms created
let usernames = {};
let rooms = ["global", "chess", "video-games"];
io.on("connection", function(socket) {
console.log("User connected to server.");
socket.on("createUser", function(username) {
socket.username = username;
usernames[username] = username;
socket.currentRoom = "global";
socket.join("global");
socket.emit("updateChat", "INFO", "You have joined global room");
socket.broadcast
.to("global")
.emit("updateChat", "INFO", username + " has joined global room");
io.sockets.emit("updateUsers", usernames);
socket.emit("updateRooms", rooms, "global");
});
socket.on("sendMessage", function(data) {
io.sockets
.to(socket.currentRoom)
.emit("updateChat", socket.username, data);
db.collection('chat').insertOne(doc, function (error, response) {
if(error) {
console.log('Error occurred while inserting');
// return
} else {
console.log('inserted record', response.ops[0]);
// return
}
});
});
socket.on("createRoom", function(room) {
if (room != null) {
rooms.push(room);
io.sockets.emit("updateRooms", rooms, null);
}
});
socket.on("updateRooms", function(room) {
socket.broadcast
.to(socket.currentRoom)
.emit("updateChat", "INFO", socket.username + " left room");
socket.leave(socket.currentRoom);
socket.currentRoom = room;
socket.join(room);
socket.emit("updateChat", "INFO", "You have joined " + room + " room");
socket.broadcast
.to(room)
.emit("updateChat", "INFO", socket.username + " has joined " + room + " room");
});
socket.on("disconnect", function() {
delete usernames[socket.username];
io.sockets.emit("updateUsers", usernames);
socket.broadcast.emit("updateChat", "INFO", socket.username + " has disconnected");
});
});