forked from cs4241-21a/final_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
85 lines (69 loc) · 2.38 KB
/
app.js
File metadata and controls
85 lines (69 loc) · 2.38 KB
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
84
85
var express = require('express');
var path = require('path');
var logger = require('morgan');
const serveStatic = require('serve-static');
const favicon = require('serve-favicon');
const fs = require('fs');
/**
* WebSocket Config
*/
const http = require('http');
const WebSocket = require('ws');
const port = 3323
const server = http.createServer(app);
const wss = new WebSocket.Server({ server })
const clients = []
let canvas
wss.on('connection', client => {
client.send(JSON.stringify(canvas))
client.on('message', message => {
console.log('Received message => ' + JSON.parse(message).width + ", " + JSON.parse(message).height)
canvas = JSON.parse(message)
clients.forEach(c => {
if (c !== client) {
c.send(JSON.stringify(canvas))
}
})
})
clients.push(client)
// var interval = setInterval(function(){
// data = "Real-Time Update "+number;
// console.log("SENT: "+data);
// clients.forEach( c => c.send( data ) )
// number++;
// }, randomInteger(2,9)*100);
client.on('close', function close() {
// clearInterval(interval);
});
})
server.listen(port, () => {
console.log(`Listening at http://localhost:${port}`)
})
//End WebSocket Config
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var loginRouter = require('./routes/login');
const bodyParser = require('body-parser');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.set('views', path.join(__dirname, 'public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
console.log("Adding routers")
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/login', loginRouter)
app.use(express.static(path.join(__dirname, 'public'))); //Needs to be at end or you need to change the homepage file to something that's not index.html
app.use(serveStatic('public'));
app.post('/api/sendImage', bodyParser.json(), function (req, res) {
let img = req.body.img.replace(/^data:image\/png;base64,/, "")
let buf = Buffer.from(img, 'base64')
fs.writeFile('./public/canvasState.png', buf, function () {
console.log('saved file')
res.send("Got it, boss")
})
})
module.exports = app;