diff --git a/InOutBoardServer/.idea/workspace.xml b/InOutBoardServer/.idea/workspace.xml index 9538d05..19eeadd 100644 --- a/InOutBoardServer/.idea/workspace.xml +++ b/InOutBoardServer/.idea/workspace.xml @@ -2,16 +2,16 @@ + - - - + + - + @@ -82,12 +83,24 @@ + + + diff --git a/InOutBoardServer/WsSetup.js b/InOutBoardServer/WsSetup.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/InOutBoardServer/WsSetup.js @@ -0,0 +1 @@ + diff --git a/InOutBoardServer/app.js b/InOutBoardServer/app.js index d32bebd..3a0097c 100644 --- a/InOutBoardServer/app.js +++ b/InOutBoardServer/app.js @@ -3,8 +3,9 @@ var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); var bodyparser = require('body-parser'); -var indexRouter = require('./routes/index'); + var usersRouter = require('./routes/users'); +var debug = require('debug')('inoutboardserver:server'); var app = require('express')(); @@ -14,13 +15,71 @@ app.use(bodyparser.urlencoded({extended: false})); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -/** - * Event listener for HTTP server "error" event. - */ +var port = normalizePort(process.env.PORT || '3000'); +function normalizePort(val) { + var port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + return false; +} +app.set('port', port); + +var server = require('http').Server(app); +var io = require('socket.io')(server); + +var indexRouter = require('./routes/index').setup(io); app.use('/', indexRouter); app.use('/api/users', usersRouter); +io.on('connection', function(socket){ + console.log('a user connected'); +}); + +server.listen(port); +server.on('error', onError); +server.on('listening', onListening); + +function onError(error) { + if (error.syscall !== 'listen') { + throw error; + } + + var bind = typeof port === 'string' + ? 'Pipe ' + port + : 'Port ' + port; + + // handle specific listen errors with friendly messages + switch (error.code) { + case 'EACCES': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +function onListening() { + var addr = server.address(); + var bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + debug('Listening on ' + bind); +} + module.exports = app; diff --git a/InOutBoardServer/bin/www b/InOutBoardServer/bin/www index 9e3e59e..d5de6c9 100644 --- a/InOutBoardServer/bin/www +++ b/InOutBoardServer/bin/www @@ -4,97 +4,30 @@ * Module dependencies. */ -var app = require('../app'); -var debug = require('debug')('inoutboardserver:server'); -var http = require('http'); -/** - * Get port from environment and store in Express. - */ -var port = normalizePort(process.env.PORT || '3000'); -app.set('port', port); /** - * Create HTTP server. + * Listen on provided port, on all network interfaces. */ -var server = require('http').Server(app); -var io = require('socket.io')(server); -/** - * Listen on provided port, on all network interfaces. - */ -server.listen(port); -server.on('error', onError); -server.on('listening', onListening); /** * Normalize a port into a number, string, or false. */ -function normalizePort(val) { - var port = parseInt(val, 10); - - if (isNaN(port)) { - // named pipe - return val; - } - - if (port >= 0) { - // port number - return port; - } - return false; -} /** * Event listener for HTTP server "error" event. */ -function onError(error) { - if (error.syscall !== 'listen') { - throw error; - } - - var bind = typeof port === 'string' - ? 'Pipe ' + port - : 'Port ' + port; - - // handle specific listen errors with friendly messages - switch (error.code) { - case 'EACCES': - console.error(bind + ' requires elevated privileges'); - process.exit(1); - break; - case 'EADDRINUSE': - console.error(bind + ' is already in use'); - process.exit(1); - break; - default: - throw error; - } -} + /** * Event listener for HTTP server "listening" event. */ -function onListening() { - var addr = server.address(); - var bind = typeof addr === 'string' - ? 'pipe ' + addr - : 'port ' + addr.port; - debug('Listening on ' + bind); -} - -io.on('connection', function (socket) { - socket.emit('news', { hello: 'world' }); - socket.on('my other event', function (data) { - console.log(data); - }); -}); - -module.exports = io; + diff --git a/InOutBoardServer/public/index.html b/InOutBoardServer/public/index.html index 400b031..d23d98f 100644 --- a/InOutBoardServer/public/index.html +++ b/InOutBoardServer/public/index.html @@ -12,9 +12,8 @@
diff --git a/InOutBoardServer/public/javascripts/frontend.js b/InOutBoardServer/public/javascripts/frontend.js index d9fa723..aa28e2b 100644 --- a/InOutBoardServer/public/javascripts/frontend.js +++ b/InOutBoardServer/public/javascripts/frontend.js @@ -6,3 +6,7 @@ function checkedInElementCreator(name,timestamp){ } +function connect() { + +} + diff --git a/InOutBoardServer/routes/index.js b/InOutBoardServer/routes/index.js index 3f04aef..d654a08 100644 --- a/InOutBoardServer/routes/index.js +++ b/InOutBoardServer/routes/index.js @@ -2,57 +2,68 @@ var express = require('express'); var router = express.Router(); var db = require('..\\database'); -/* GET home page. */ -router.get('/', function (req, res, next) { - res.render('index', {title: 'Express'}); -}); +function setup(io) { + router.get('/', function (req, res, next) { + res.render('index', {title: 'Express'}); + }); -router.get('/api/status', function (req, res, next) { - db.many(`SELECT DISTINCT ON (userid) userid, status, dt, tuser.name FROM tevent INNER JOIN tuser AS tuser ON tevent.userid = tuser.id ORDER BY userid, dt DESC`) - .then(data => { - res.status(200).json(data); - }).catch(err => { - res.status(404).send(); - }) -}); - -router.get('/api/status/:id', function (req, res, next) { - db.one(`SELECT tevent.id, userid, status, dt, tuser.name FROM tevent INNER JOIN tuser AS tuser ON tevent.userid = tuser.id WHERE userid = $1 ORDER BY dt DESC LIMIT 1`, req.params.id) - .then(data => { - res.status(200).json(data); - }).catch((err) => { - res.status(404).send(); - }) -}); -router.post('/api/in/:id', function (req, res, next) { - insertEvent(req, status.IN) - .then(data => { - res.status(200).json(data); + router.get('/ws', function (req, res, next) { + io.emit("update", {data: "this is an update from the server"}); + res.status(200).send(); + }); + + + router.get('/api/status', function (req, res, next) { + db.many(`SELECT DISTINCT ON (userid) userid, status, dt, tuser.name FROM tevent INNER JOIN tuser AS tuser ON tevent.userid = tuser.id ORDER BY userid, dt DESC`) + .then(data => { + res.status(200).json(data); + }).catch(err => { + res.status(404).send(); }) - .catch((err) => { - console.log(err); - res.status(500).send(err); - }); -}); - -router.post('/api/out/:id', function (req, res, next) { - insertEvent(req, status.OUT) - .then(data => { - res.status(200).json(data); + }); + + router.get('/api/status/:id', function (req, res, next) { + db.one(`SELECT tevent.id, userid, status, dt, tuser.name FROM tevent INNER JOIN tuser AS tuser ON tevent.userid = tuser.id WHERE userid = $1 ORDER BY dt DESC LIMIT 1`, req.params.id) + .then(data => { + res.status(200).json(data); + }).catch((err) => { + res.status(404).send(); }) - .catch((err) => { - console.log(err); - res.status(500).send(err); - }); -}); + }); + + router.post('/api/in/:id', function (req, res, next) { + insertEvent(req, status.IN) + .then(data => { + res.status(200).json(data); + }) + .catch((err) => { + console.log(err); + res.status(500).send(err); + }); + }); + + router.post('/api/out/:id', function (req, res, next) { + insertEvent(req, status.OUT) + .then(data => { + res.status(200).json(data); + }) + .catch((err) => { + console.log(err); + res.status(500).send(err); + }); + }); + + return router; + +} function insertEvent(req, status) { return db.one(`INSERT INTO tevent (userid, status) VALUES ($1,'${status}') RETURNING *`, req.params.id) } -module.exports = router; +module.exports.setup = setup; const status = { IN: "IN", diff --git a/InOutBoardServer/ws.js b/InOutBoardServer/ws.js deleted file mode 100644 index e69de29..0000000