-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (60 loc) · 1.81 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
75
76
77
78
79
80
81
82
83
/*************************************
//
// sockets app
//
**************************************/
'use strict';
// express magic
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var device = require('express-device');
var Comment = require('./routes/comment');
var runningPortNumber = process.env.PORT;
// assuming io is the Socket.IO server object
io.configure(function () {
io.set('transports', ['xhr-polling']);
io.set('polling duration', 10);
});
app.configure(function () {
// I need to access everything in '/public' directly
app.use(express.static(__dirname + '/public'));
//set the view engine
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(device.capture());
});
// logs every request
app.use(function (req, res, next) {
// output every request in the array
console.log({method: req.method, url: req.url, device: req.device});
// goes onto the next function in line
next();
});
app.get('/', function (req, res) {
res.render('index', {});
});
var readerCount = 0;
io.sockets.on('connection', function (socket) {
readerCount++;
io.sockets.emit('readerCount', {count: readerCount});
Comment.find(Date.now()).then(function (comments) {
socket.emit('posts', comments);
});
socket.on('post', function (data, fn) {
var comment = new Comment.Post(data.handle, data.comment, data.homepage);
comment.save();
io.sockets.emit('blast', comment);
fn();//call the client back to clear out the field
});
socket.on('disconnect', function () {
readerCount--;
io.sockets.emit('readerCount', {count: readerCount});
});
});
io.sockets.on('disconnect', function () {
readerCount--;
io.sockets.emit('readerCount', {count: readerCount});
});
server.listen(runningPortNumber);