-
Notifications
You must be signed in to change notification settings - Fork 67
/
server.js
executable file
·105 lines (85 loc) · 3.17 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
//=============================================================================
// Nelderson's Online Core Server
// Version: 0.2.2 - Added abilty to limit users to one login.
// Version: 0.2.1
//=============================================================================
var express = require('express');
var app = express();
var server = require('http').createServer(app)
var io = require('socket.io')(server);
var sticky = require('sticky-session');
var config = require('./configurations/config');
var bodyParser = require('body-parser');
var logger = require('morgan'); //For development
var log = require('tracer').colorConsole(config.loggingConfig);
var loggedInUsers = {};
//Database Connction
require('./api_routes/loginDBConnection')();
var auth = require('./auth.js');
const redisAdapter = require('socket.io-redis');
io.adapter(redisAdapter(config.redisConnection));
app.use(logger('dev'));//For development
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(function(req,res,next){
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
if (!sticky.listen(server, config.port, {workers: process.env.MV_WORKER_COUNT || null})) {
// Master code
server.once('listening', function() {
log.info('Master Server started on port: '+ config.port);
});
}
else {
// Worker code
log.info('Worker is on bruh and running on port: '+ config.port);
//----------------------------------
// SET ROUTES FOR EXPRESS API HERE:
//----------------------------------
// No Authentication required:
app.use('/',require('./api_routes/login_routes'));
app.use('/metrics',require('./api_routes/metrics'));
// Authentication required:
app.use('/cloudsave',require('./api_routes/cloudsave.js'));
//Static Server - Used to serve static files (HTNL,PNG,etc.)
app.use('/static', express.static('public'));
//----------------------------------
// ADD SOCKET IO MODULES HERE:
//----------------------------------
// var exampleSocket = require('./socket_modules/exampleSocket');
var netplayers = require('./socket_modules/netplayer');
var chat = require('./socket_modules/chat');
var globalvar = require('./socket_modules/globalvar');
//Authorize socket connection with token from login
io.use(auth.authSocket)
//When first connected to Socket.io
io.on('connection', function(socket){
if(config.enforceOneUser){
var username = socket.user.name;
if (loggedInUsers[username]){
socket.to(loggedInUsers[username]).emit('firstShutDown',{});
socket.emit('secondShutDown',{});
io.of('/').connected[loggedInUsers[username]].disconnect(true);
socket.disconnect(true);
}
else {
loggedInUsers[username] = socket.id;
}
}
io.clients(function(error, clients){
if (error) throw error;
log.info("There are " + clients.length + " players connected");
});
socket.on('disconnect',function(data){
loggedInUsers[username] = null;
});
});
//----------------------------------
// BIND SOCKET IO MODULES HERE:
//----------------------------------
// exampleSocket(io);
netplayers(io);
chat(io);
globalvar(io);
} // <---Don't delete