-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
131 lines (103 loc) · 4.07 KB
/
index.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
require('dotenv').config();
const express = require('express');
const io = require('socket.io');
const path = require('path');
const request = require('request');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 5000;
const compression = require('compression');
const minify = require('express-minify');
const uglifyEs = require('uglify-es');
const app = express();
if (app.settings.env !== "development") {
const enforce = require('express-sslify');
app.use(enforce.HTTPS({trustProtoHeader: true}));
}
app.use(compression());
app.use(minify({uglifyJsModule: uglifyEs,}));
let ENusers = [];
let KOusers = [];
let EditingUsers = new Array(16).fill(0);
let server = app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600 }))
.use(bodyParser.json()) // support json encoded bodies
.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/main', {URL: process.env.URL, SCORE:process.env.SCORE}))
.get('/en/', (req, res) => res.render('pages/index', { REGION : "en" , URL: process.env.URL, SCORE:process.env.SCORE, DATE: ""}))
.get('/en/:date/', (req, res) => res.render('pages/index', { REGION : "en" , URL: process.env.URL, SCORE:process.env.SCORE, DATE: req.params.date}))
// .get('/ko/', (req, res) => res.render('pages/index', { REGION : "ko" , URL: URL}))
// .post('/api/', function(req, res) {
//
// request.post({
// url: keys.URL,
// body: req.body,
// json: true
// }, function(error, response, body){
// if (body) {
// console.log(body);
// res.send(JSON.stringify(body));
// }
// else
// res.send(error);
// });
//
// })
.get('*', (req, res) => res.redirect("/"))
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
let sio = io.listen(server);
const io_english = sio.of('/en');
// const io_korean = sio.of('/ko');
io_english.on('connection', function(socket){
// Increase user number
ENusers.push(socket.id);
// Init people & broadcast entering.
socket.emit('init', ENusers, EditingUsers);
socket.broadcast.emit('enter', socket.id);
console.log('[EN] user '+ socket.id +' connected');
// On Disconnect
socket.on('disconnect', function(){
console.log('[EN] user ' + socket.id + ' disconnected');
ENusers = removeFromArray(ENusers, socket.id);
let i = EditingUsers.indexOf(socket.id);
if (i > -1) {
EditingUsers[i] = 0;
}
socket.broadcast.emit('exit', socket.id, i);
});
socket.on('startWriting', function(index){
if (index < EditingUsers.length) {
EditingUsers[index] = socket.id;
socket.broadcast.emit('startWriting', socket.id, index);
} else if (index >= EditingUsers.length) {
socket.broadcast.emit('startWriting', socket.id, index);
}
});
socket.on('cancelWriting', function(index){
socket.broadcast.emit('cancelWriting', socket.id, index);
EditingUsers = removeAndReplaceArrayEl(EditingUsers, socket.id, 0);
});
socket.on('finishWriting', function(index, isInsert){
socket.broadcast.emit('finishWriting', socket.id, index, isInsert);
EditingUsers = removeAndReplaceArrayEl(EditingUsers, socket.id, 0);
});
});
function removeFromArray(arr, el) {
let index = arr.indexOf(el);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
function removeAndReplaceArrayEl(arr, el, replaceTo) {
let index = arr.indexOf(el);
if (index > -1) {
arr[index] = replaceTo;
}
return arr;
}
function getIpFromReq (req) { // get the client's IP address
let bareIP = ":" + ((req.connection.socket && req.connection.socket.remoteAddress)
|| req.headers["x-forwarded-for"] || req.connection.remoteAddress || "");
return (bareIP.match(/:([^:]+)$/) || [])[1] || "127.0.0.1";
}