Skip to content

Commit

Permalink
fuck this shit socket.io and node is stupid and a mistake /rant over
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugzy committed Mar 10, 2020
1 parent a131e48 commit f6826d3
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 122 deletions.
21 changes: 17 additions & 4 deletions InOutBoardServer/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions InOutBoardServer/WsSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

67 changes: 63 additions & 4 deletions InOutBoardServer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')();

Expand All @@ -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;
73 changes: 3 additions & 70 deletions InOutBoardServer/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -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;

5 changes: 2 additions & 3 deletions InOutBoardServer/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
socket.on('update', function (data) {
console.table(data);
});
</script>
<div class="card border-primary mb-3" style="width: 15rem;">
Expand Down
4 changes: 4 additions & 0 deletions InOutBoardServer/public/javascripts/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ function checkedInElementCreator(name,timestamp){

}

function connect() {

}

93 changes: 52 additions & 41 deletions InOutBoardServer/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Empty file removed InOutBoardServer/ws.js
Empty file.

0 comments on commit f6826d3

Please sign in to comment.