-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
88 lines (67 loc) · 2.61 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
var fs = require('fs');
const jsonServer = require('json-server');
const jsServer = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
const expressSession = require('express-session');
const sharedSession = require("express-socket.io-session");
const Auth = require('./auth');
// Set default middlewares (logger, static, cors and no-cache)
let session = expressSession({
secret: '63?gdº93!6dg36dºb36%Vv57V%c$%/(!V497',
resave: false,
saveUninitialized: true,
cookie: {secure: false}
});
jsServer.use(session);
jsServer.use(middlewares);
jsServer.use(jsonServer.bodyParser);
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
let configuredRouter = Auth.configure(router, config);
jsServer.use('/api', configuredRouter);
http_server = require('http').createServer(jsServer);
if (config.service)
if (config.service === 'peerjs') initPJS();
else initWS();
let port = config.port || 3000;
http_server.listen(port, () => {
console.log('JSON Server is running on port ' + port)
});
function initWS() {
console.log('Starting socket.io server ...');
// Web Sockets
const socketIO = require('socket.io')(http_server);
socketIO.use(sharedSession(session));
const wsConnections = {};
socketIO.on('connection', function (socket) {
console.log('Websocket connection');
let username = socket.handshake.session.username;
if (!wsConnections[username]) wsConnections[username] = socket;
socket.on('connected', function (data) {
console.log("Connected!");
})
socket.on('message', function (data) {
let kk = socketIO.clients;
if (socket.handshake.session.username) {
const destSocket = wsConnections[data.dest];
if (destSocket) {
console.log('Message: ' + data.message + ' from user ' + username + ', to ' + data.dest);
wsConnections[data.dest].emit('message', {
message: data.message,
from: socket.handshake.session.username
});
}
} else {
console.log('User not logged in. Message: ' + data.message + ', to ' + data.dest);
}
});
});
}
function initPJS() {
console.log('Starting peerjs server ...');
const app_ps = require('peer').ExpressPeerServer(http_server, {debug: false, allow_discovery: true});
jsServer.use('/peerjs', app_ps);
app_ps.on('connection', function (id) {
console.log('Peerjs connection with id=' + id)
});
}