-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (129 loc) · 3.97 KB
/
index.js
File metadata and controls
175 lines (129 loc) · 3.97 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 5000;
const { Sequelize, DataTypes } = require('sequelize');
const WebSocketServer = require("ws").Server
const http = require("http");
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.urlencoded());
// npm run build in react project
const buildFrontPath = path.join(__dirname, 'dpm', 'build');
app.use(express.static(buildFrontPath));
app.get('/api', (req, res) => {
res.status(200).json({
"hello" : "bonjour"
})
})
app.get('/*', (req,res) => {
res.sendFile(path.join(__dirname, 'dpm', 'build','index.html'));
})
let server = http.createServer(app);
// Should be replaced later with a database entries instead
// Currently, kind of multimap structure in mem such as :
// const clients = {
// hash1: [ws1, ws2 ...],
// hash2: [....]
// };
const clients = {};
const uploads = {};
const wss = new WebSocketServer({server: server, maxPayload: 1e+9}); // 1Gb ( converted into Kb)
console.log("WSS server created");
wss.on("connection", function(ws) {
ws.send(JSON.stringify(new Date()), function() { })
console.log("websocket connection open")
ws.on("close", function() {
console.log("websocket connection close")
});
ws.on("erro", err => {
console.log(err)
})
ws.on('message', msg => {
let data = JSON.parse(msg);
const {hash, topic} = data;
if ( topic === "join" ) {
// TODO : Make difference between new user AND existing just spamming F5 ...
if ( clients[hash] ) {
clients[hash].push(ws);
} else {
if ( hash !== "") {
clients[hash] = new Array(ws);
}
}
// Init "hub" for the uploaded files in room
if ( !uploads[hash] ) {
uploads[hash] = new Array();
} else {
// Send to new user the current files uploaded in this room
clients[hash].map( socket => socket.send(JSON.stringify({
"topic": "newUser",
"payload": "new user has join the room !",
"currentFiles" : uploads[hash]
})));
}
}
if ( topic === "uploadFiles" ) {
const { blobFiles } = data;
if ( blobFiles.length > 0 ) {
blobFiles.map( blob => {
uploads[hash].push(blob);
});
// notify clients in room
clients[hash].map( socket => socket.send(JSON.stringify({
"topic": "downloadable",
"payload": uploads[hash]
})));
}
}
if ( topic === "removeFile" ) {
const {position, hash} = data;
uploads[hash].splice(position, 1);
clients[hash].map( socket => socket.send(JSON.stringify({
"topic": "downloadable",
"payload": uploads[hash]
})));
}
})
});
// DEV
if ( process.env.NODE_ENV === "DEV" ) {
console.log(`[API] starts on ${5000} - DEV`);
server.listen(5000)
} else {
console.log("STOP");
server.listen(PORT, async () => {
// heroku config:set PGSSLMODE=no-verify
try {
const sequelize = new Sequelize(`${process.env.DATABASE_URL}`, {
dialectOptions: {
ssl: { /* <----- Add SSL option */
require: true,
rejectUnauthorized: false
}
}
});
await sequelize.authenticate();
// DIRTY TEST
const User = sequelize.define('User', {
// Model attributes are defined here
firstName: {
type: DataTypes.STRING,
allowNull: false
},
lastName: {
type: DataTypes.STRING
// allowNull defaults to true
}
}, {
// Other model options go here
});
await sequelize.sync({force: true});
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
console.log(`server started on port ${PORT}`);
})
}