Commit bd6c0519 authored by Jan Grewe's avatar Jan Grewe
Browse files

use WebSockets to tell frontend about new photos

parent 5be8e0de
Pipeline #4 passed with stage
......@@ -21,22 +21,21 @@ var net = require('net');
var dgram = require('dgram');
var fs = require('fs');
var gm = require('gm');
/* Express */
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
var routes = require('./routes/index');
var latestPhoto = require('./routes/latest');
var allPhotos = require('./routes/all');
var app = express();
process.title = 'WiPho';
var gracefulShutdown = function() {
console.log("Shutting down...");
......@@ -46,6 +45,8 @@ var gracefulShutdown = function() {
process.on ('SIGTERM', gracefulShutdown);
process.on ('SIGINT', gracefulShutdown);
app.set('port', config.httpPort || 3000);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
......@@ -95,6 +96,25 @@ app.use(function(err, req, res, next) {
module.exports = app;
io.on('connection', function (socket) {
//console.log('Display connected!');
socket.on('disconnect', function(){
//console.log('Display disconnected');
});
socket.on('display', function(data) {
if(data.status == 'success') {
console.log('Photo displayed!');
} else {
console.log('Photo not displayed!');
}
});
});
server.listen(app.get('port'), function() {
console.log("WiPho is listening on port " + app.get('port'));
});
/* WiPho */
photos = new Array();
......@@ -134,6 +154,7 @@ function downloadPhotos() {
if(photos.length == 0 || photo != photos[photos.length-1].name) {
photos.push({id: photoIndex, name: photo});
photoIndex++;
io.emit('photo', { path: photo });
}
}else{
......
#!/usr/bin/env node
var config = require('../config.json');
var debug = require('debug')('foo');
var app = require('../app');
app.set('port', config.httpPort || 3000);
var server = app.listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});
......@@ -3,17 +3,18 @@
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www",
"start": "node app.js",
"stop": "pkill --signal SIGINT WiPho"
},
"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"body-parser": "^1.12.0",
"cookie-parser": "~1.3.3",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0",
"gm": "~1.16.0"
"debug": "^0.7.0",
"express": "~4.12.2",
"gm": "^1.17.0",
"jade": "^1.9.2",
"morgan": "^1.5.1",
"serve-favicon": "^2.2.0",
"socket.io": "^1.3.5"
}
}
var socket = io.connect();
$(document).ready(function() {
$('body').height($(window).height());
centerImage();
getLatest();
setInterval(function() {
getLatest();
}, 1000);
$(window).resize(function () {
centerImage();
});
......@@ -23,17 +21,14 @@ $(document).ready(function() {
$(this).animate({opacity: 1}, 400);
});
});
function getLatest() {
$.getJSON('/latest', function(data) {
if('/previews/'+data.name != $('#latest').attr('src')) {
socket.on('photo', function (data) {
$('#latest').animate({opacity: 0}, 400, function() {
$(this).attr('src', '/previews/'+data.name);
$(this).attr('src', '/previews/'+data.path);
socket.emit('display', {status: 'success'});
});
}
});
}
});
function centerImage() {
$('#latest').css({
......
var config = require('../config.json');
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'WiPho' });
res.render('index', {
title: 'WiPho'
});
});
module.exports = router;
......@@ -2,6 +2,7 @@ doctype html
html
head
title= title
script(src='/socket.io/socket.io.js')
script(src='/javascripts/jquery-1.11.1.min.js')
script(src='/javascripts/wipho.js')
link(rel='stylesheet', href='/stylesheets/wipho.css')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment