-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·124 lines (108 loc) · 3.86 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
var app = require('express')();
var express = require('express')
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clients = [];
var regUsers = [];
var chatRoomName;
const MongoClient = require('mongodb').MongoClient;
var db;
var collectionID;
app.use(express.static('public'));
MongoClient.connect('mongodb://root:root@ds155841.mlab.com:55841/sliit-chat-mtit', (err, database) => {
if (err) return console.log(err)
db = database
/*
*process.env.PORT lets the port to be set dynamically depending
*on the environmnent the server is hosted.
*/
var port = process.env.PORT || 8080;
http.listen(port, function() {
console.log('listening on ' + port);
});
// http.listen(3000, function() {
// console.log('listening on *:3000');
// });
})
/**
* Routing to the root page.
*/
app.get('/', function(req, res) {
console.log('GET /');
res.sendFile(__dirname + '/index.html');
});
/**
* This event will be called when a user lands on the root page of the chatroom.
*/
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
});
/**
* This event will be fired when a user tries to connect to a chat room.
* If the user is a new user. This function will send a default message to the new user.
*/
socket.on('register', function(msg, response) {
if (clients.indexOf(socket.id) == -1) {
clients.push(socket.id);
regUsers.push({ 'name': msg.name, 'id': socket.id });
console.log('Registered ' + msg.name);
chatRoomName = msg.chatRoomName;
response('success');
io.to(socket.id).emit('chat message', {
'message': 'Welcome to SLIIT chat ' + msg.name +
'. You have joined chatroom ' + msg.chatRoomName,
'from': 'SLIIT Bot',
'timestamp': ' '
});
}
});
/**
* This event will be fired after a user successfully joins a chatroom.
* If a chat history exists in the chat room, retrieve the history and send to the frontend.
*/
socket.on('retrieve history', function(msg) {
console.log('retrieve history by: ' + msg.from);
db.collection(chatRoomName).find().toArray(function(err, results) {
console.log(results)
io.to(socket.id).emit('chat history', results);
})
});
/**
* This event will be fired when a user tries to join or create a chatroom.
* Checks if a chatroom exists.
*/
socket.on('check availability', function(msg, response) {
console.log('join chatroom: ' + msg.from);
db.collection(msg.chatRoomName).find().toArray(function(err, results) {
console.log('availability: ' + results)
if (results.length == 0) {
response('error', results);
} else {
response('success', results);
}
})
});
/**
* This event will be called whenever a user enters a message and a chat is emmitted.
*/
socket.on('chat message', function(msg) {
db.collection(chatRoomName).save(msg, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
})
console.log('message: ' + msg.message);
var username = '';
for (var i = 0; i < regUsers.length; i++) {
if (regUsers[i].id == socket.id) {
username = regUsers[i].name;
console.log('username found');
}
}
console.log('emmitting from: ' + username);
io.emit('chat message', { 'message': msg.message, 'from': username });
console.log('No of clients: ' + clients.length);
console.log('Clients: ' + clients);
});
});