-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (51 loc) · 1.72 KB
/
server.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
68
69
70
71
const http = require('http');
const express = require('express');
const socket_io = require('socket.io');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cors = require('cors')
const config = require('./config/database');
const app = express();
// Parameters
const port = process.env.PORT || 9080;
// Database connection
mongoose.connect(config.database, { useNewUrlParser: true });
mongoose.connection.on('connected', () => {
console.log('Connected to database '+config.database);
});
mongoose.connection.on('error', (err) => {
console.log('Database error '+err);
});
// Body Parser Middleware
app.use(bodyParser.json());
app.use(cors())
// Http server
console.log("Configureing Http server")
const server = http.Server(app);
const io = socket_io(server);
// ########## Done server constants ##########
const users = require('./routes/users');
const trackers = require('./routes/trackers');
const login = require('./routes/login');
const webapp_io = io.of('/webapp_io');
app.use('/login', login);
app.use('/users', users);
app.use('/trackers', trackers(webapp_io))
// When client connects to websocket
webapp_io.on('connection', function (socket) {
// Join a room or create one
// const username = socket.handshake.query.username
const username = "main"
const room = username
socket.join(room);
socket.emit('message', { message: 'Connected to room ' + username });
console.log(username + ' joined room ' + room)
// When client update settings
// socket.on('settings update', function (data) {
// console.log(data);
// socket.emit('message', { msg: 'settings updated' });
// });
});
server.listen(port, () => {
console.log('Server started on port '+port);
});