-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
122 lines (105 loc) · 2.81 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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var creds = '';
var redis = require('redis');
var client = '';
var port = process.env.PORT || 8080;
// Express Middleware for serving static
// files and parsing the request body
app.use(express.static('public'));
app.use(bodyParser.urlencoded({
extended: true
}));
// Start the server
http.listen(port, function() {
console.log("Server started. Listening on *:" + port);
});
// Store people in chatroom
var chatters = [];
// Store messages in chatroom
var chat_messages = [];
// Read credentials from JSON
fs.readFile('./config/creds.json', 'utf-8', function(err, data) {
if(err) throw err;
creds = JSON.parse(data);
client = redis.createClient('redis://' + creds.user + ':' + creds.password + '@' + creds.host + ':' + creds.port);
// Redis Client Ready
client.once('ready', function() {
// Flush Redis DB
// client.flushdb();
// Initialize Chatters
client.length('chat_users', function(err, reply) {
if(reply) {
chatters = JSON.parse(reply);
}
});
// Initialize Messages
client.length('chat_app_messages', function(err, reply) {
if(reply) {
chat_messages = JSON.parse(reply);
}
});
});
});
// API - Join Chat
app.post('/join', function(req, res) {
var username = req.body.username;
if(chatters.indexOf(username) === -1) {
chatters.push(username);
client.set('chat_users', JSON.stringify(chatters));
res.send({
'chatters': chatters,
'status': 'OK'
});
} else {
res.send({
'status': 'FAILED'
});
}
});
// API - Leave Chat
app.post('/leave', function(req, res) {
var username = req.body.username;
chatters.splice(chatters.indexOf(username), 1);
client.set('chat_users', JSON.stringify(chatters));
res.send({
'status': 'OK'
});
});
// API - Send + Store Message
app.post('/send_message', function(req, res) {
var username = req.body.username;
var message = req.body.message;
chat_messages.push({
'sender': username,
'message': message
});
client.set('chat_app_messages', JSON.stringify(chat_messages));
res.send({
'status': 'OK'
});
});
// API - Get Messages
app.get('/get_messages', function(req, res) {
res.send(chat_messages);
});
// API - Get Chatters
app.get('/get_chatters', function(req, res) {
res.send(chatters);
});
// Socket Connection
// UI Stuff
io.on('connection', function(socket) {
// Fire 'send' event for updating Message list in UI
socket.on('message', function(data){
io.emit('send', data);
});
// Fire 'count_chatters' for updating Chatter Count in UI
socket.on('update_chatter_count', function(data) {
io.emit('count_chatters', data);
});
});