-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (52 loc) · 1.73 KB
/
index.js
File metadata and controls
81 lines (52 loc) · 1.73 KB
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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);
var request = require('request');
var compression = require('compression');
// compress all responses
app.use(compression())
var port = process.env.PORT || 5000;
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
var uri = 'https://www.reddit.com/r/nba/top.json?limit=10';
app.get('/', function(req, res){
res.sendFile(__dirname + '/views/index.html');
});
// setInterval(function (){
// request(uri, function (error, res, body){
// if(!error && res.statusCode == 200) {
// var jsonObj = JSON.parse(body);
// var topics = [];
// jsonObj.data.children.forEach(function(child) {
// console.log('Title : ' + child.data.title);
// console.log('URL : ' + child.data.url);
// var topic = {'title' : child.data.title, 'url' : child.data.url};
// topics.push(topic);
// });
// var rand = Math.floor(Math.random()*topics.length);
// io.emit('topic change', topics[rand]);
// }
// })
// }
// , 10000);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('msg', function(msg) {
if (typeof(msg) === 'object' && containsValidText(msg))
io.emit('msg', msg);
});
socket.on('disconnect', function() {
console.log('user disconnected');
});
});
http.listen(port, function() {
console.log('Node app is running on port', port);
});
//Takes object as parameter and checks if it contains valid text
function containsValidText(msg) {
if (msg.hasOwnProperty('text') && typeof(msg.text) === 'string')
return true;
return false;
}