forked from amigame/tadpole
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
74 lines (62 loc) · 1.97 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
// Include important JS helpers
require('./helpers.js');
var express = require('express'), // Include express engine
serveStatic = require('serve-static'), // Serve static files
app = express(), // create node server
bodyParser = require('body-parser'),
config = require('./config/app.js');
var server = require('http').createServer(app);
var io = require('socket.io')(server);
// Default APP Configuration
app.set('view engine', 'jade'); // uses JADE templating engine
app.set('views', __dirname + '/views'); // default dir for views
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(serveStatic('public/', {'index': ['index.html', 'index.htm']}))
// Index Route
app.get('/', function(req, res){
res.render('index', {
locals: {
title: "Rumpetnode! It's Rumpetroll with a node backend"
}
});
});
// Auth Route
app.get('/auth', function(req, res){
res.render('auth', {
locals: {
title: "Authenticate Twitter",
twitter: config.twitter,
layout: false
}
});
});
// Listen on this port
app.listen(8000);
// Socket Connection
var clients = []; // List of all connected Clients
// When user gets connected
io.on('connection', function(client){
// new client is here!
var index = clients.push(client) - 1; // get array index of new client
// On Message, send message to everyone
client.on('message', function(data){
console.log('got message ==> ' + data);
data = JSON.parse(data); // parse string data to json
for(var i=0;i<clients.length;i++) {
try {
if(clients[i] != undefined)
clients[i].send(data.msg); // send to all connected clients
} catch(e) {
console.log("doesn`t exist");
continue; //if a client doesn`t exist, jus continue;
}
}
});
client.on('disconnect', function(){
clients.splice(index,1); // remove client from array
console.log("after length ===> " +clients.length);
});
});