-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
67 lines (58 loc) · 1.58 KB
/
config.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
"use strict";
var express = require('express');
var bodyParser = require('body-parser');
var options = {
extensions: ['htm', 'html'],
maxAge: '1d',
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now())
}
};
exports.initApp = function() {
var app = express();
app.use(express.static(__dirname + '/static', options));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(function (peticion, respuesta, siguiente) {
console.log("recibida petición: " + peticion.method + " " + peticion.url);
if (peticion.body) {
console.log("body: " + JSON.stringify(peticion.body));
}
siguiente();
});
return app;
}
exports.initRouter = function(app) {
var router = express.Router();
app.use(router);
return router;
}
exports.initIO = function(app){
var server = require('http').Server(app);
var io = require('socket.io')(server);
var sockets = [];
function conectar(socket) {
console.log("Conectando con: " + socket.client.conn.remoteAddress);
sockets.push(socket);
var saludo = {
serverPid: process.pid,
date: new Date()
};
socket.emit('wellcome', saludo);
console.log("Enviado saludo " + JSON.stringify(saludo));
socket.on('postedData', function (data) {
console.log("Un cliente ha actualizado algo" );
emitirCanalMensaje("updateTutti",saludo);
});
}
io.on("connect", conectar);
function emitirCanalMensaje(canal, mensaje) {
console.log(new Date().toLocaleTimeString() + " " + canal + " : " + mensaje);
sockets.forEach(function (socket) {
socket.emit(canal, mensaje);
});
}
return server;
}