-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
132 lines (104 loc) · 2.89 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* server.js
*
* The main server dispatcher.
*
*/
require('dotenv').config();
const {
CONN_STATUS
} = require('./constants')
const {
WAITING,
PAIRING,
DISCONNECTED
} = CONN_STATUS;
// to connect to the database and instantiate the data models
var db = require('./db/connect');
var server = require("http").createServer(app);
// Authentication
var passport = require('passport');
// Parsing post requests
var bodyParser = require('body-parser');
var multer = require('multer');
// ----------------
// Begin Merged code
// ----------------
/*global console*/
var config = require('getconfig'),
fs = require('fs'),
sockets = require('./sockets'),
express = require('express');
var app = express();
var server = require("http").createServer(app);
var io = require("socket.io").listen(server);
// ----------------
// End Merged code
// ----------------
// to use the handlebars templating engine
var exphbs = require('express-handlebars');
app.engine('handlebars', exphbs({
defaultLayout: 'main',
layoutsDir: __dirname + '/views/layouts/',
partialsDir: __dirname + '/views/partials/'
}));
app.set('view engine', 'handlebars');
const session = require("express-session")({
secret: "my-secret",
resave: true,
saveUninitialized: true
});
const sharedsession = require("express-socket.io-session");
app.use(require('cookie-parser')());
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(require('express-session')({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
app.use(session);
app.use(passport.initialize());
app.use(passport.session());
io.use(sharedsession(session, {
autoSave:true
}));
// to listen to port 3000
server.listen(process.env.PORT || 3000)
const Matcher = require('./matcher');
let matcher = new Matcher((id, status, partner = null, room = null) => {
switch (status) {
case WAITING:
{
// io.sockets.emit("meta", `${id} is waiting`)
io.to(id).emit("waiting");
break;
}
case PAIRING:
{
// io.sockets.emit("meta", `${id} is pairing to ${partner}`)
io.to(id).emit("pairing", matcher.getUsername(partner), room);
break;
}
case DISCONNECTED:
{
io.to(id).emit("disconnected")
}
default:
{
break;
}
}
})
// add callbacks to matcher
const chat = require('./controllers/chats');
matcher.addCallback(PAIRING, chat.logConnection);
matcher.addCallback(DISCONNECTED, chat.logDisconnection);
sockets(server, io, matcher, config);
// Require our routes
const mainRoute = require('./routes/main')
app.use('/', mainRoute);
const createUtilRoute = require('./routes/util');
const utilRoute = createUtilRoute(matcher)
app.use('/util', utilRoute);
// Serve static resources
app.use('/static', express.static('static'))