-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (40 loc) · 1.24 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
var app = require('http').createServer(handler)
, staticserver = require('node-static')
, file = new staticserver.Server()
, twitter = require('ntwitter')
, io = require('socket.io').listen(app)
, twitcreds = require('./twittercreds.js');
// some local vars:
var iTwitCount = 0;
// start listening:
app.listen(8080);
// http handler (mainly static content serving):
function handler(request, response){
// serve files:
request.addListener('end', function(){
file.serve(request, response);
});
}
// Socket handler:
io.sockets.on('connection', function(socket){
console.log('new connection');
});
// twitter:
var bUseTwitter = process.env.NODE_ENV === 'production';
if (bUseTwitter){
// set io log level (so we don't fill the log with debug):
io.set('log level', 1);
// get data from twitter:
var tw = new twitter(twitcreds).stream('statuses/sample'
, function(stream){
stream.on('data', function(tweet){
// send to all clients:
io.sockets.emit('data', {len:tweet.text.length});
})
});
} else {
// TEST, make data:
setInterval(function(){
io.sockets.emit('data', {len:Math.floor((Math.random()*140)+1)});
}, 2);
}