-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
176 lines (160 loc) · 5.13 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
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
173
174
175
176
require('dotenv').config();
const crypto = require('crypto');
const WebSocket = require('ws');
const mongo = require('mongodb');
const markdown = require('markdown').markdown;
const ip = require('ip');
const getInitialMessages = require('./controllers/getInitialMessages');
const saveMessage = require('./controllers/saveMessage');
const getSeedAndUpdate = require('./controllers/getSeedAndUpdate');
const getUsers = require('./controllers/getUsers');
const updateUserName = require('./controllers/updateUserName');
const usedPort = process.env.PORT || 4000;
const usedWsAddress = 'ws://' + ip.address() + ':' + usedPort;
const usedServerName = process.env.NAME || "No name";
if (!process.env.NAME) {
console.warn('! Your server name will default to "No name".\n' +
'! You can change the name by creating a file named ".env" in\n' +
'! the root of the project with the contents:\n' +
'! \tNAME="The name you want to display"');
}
const MongoClient = mongo.MongoClient;
const dbPromise = MongoClient.connect('mongodb://127.0.0.1:27017/chat')
.catch( err => console.log('Connection failed\n', err));
function* clientIdGenerator (seed = new Date().valueOf()) {
// TODO improve the seed
let hash;
while (true) {
yield dbPromise
.then(getSeedAndUpdate)
.then( uidSeed => new Promise( (resolve, reject) => {
hash = crypto.createHash('sha1');
hash.update(seed + uidSeed + '');
const id = hash.digest().toString('base64');
resolve(id);
}))
.catch( err => console.log('There was an error getting the new id'));
}
}
const idGen = clientIdGenerator();
const wss = new WebSocket.Server({ port: usedPort });
const broadcast = wsAction => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(wsAction));
}
});
}
wss.on('connection', (ws, req) => {
console.log('New connection');
const initialMessagesPromise = dbPromise.then(getInitialMessages)
.catch( err => console.log('Error getting initial messages.\n', err));
const userNamesPromise = dbPromise.then(getUsers)
.catch( err => console.log('Error getting user names.\n', err));
Promise.all([
initialMessagesPromise,
userNamesPromise
])
.then( ([initialMessages, userNames]) => {
const handshake = {
type: 'HANDSHAKE',
payload: {
serverName: usedServerName,
messages: initialMessages,
userNames
}
}
ws.send(JSON.stringify(handshake));
})
ws.on('message', (data) => {
data = JSON.parse(data);
//console.log(data);
let wsAction;
switch (data.type) {
case 'HANDSHAKE_USER_INFO':
let userId = data.payload.userId;
new Promise( (resolve, reject) => {
if (userId === undefined) {
idGen.next().value.then( id => {
const wsAction = {
type: 'NAME_CHANGE',
payload: {
userId: id,
name: 'Anonymous',
idNamePair: { [id]: 'Anonymous' }
}
}
broadcast(wsAction);
resolve({
userId: id,
name: 'Anonymous'
});
})
} else {
console.log('user id given from front:', userId);
userNamesPromise.then( userNames => resolve({
userId,
name: userNames[userId]
}));
}
})
.then( userInfo => {
console.log(userInfo);
const wsAction = {
type: 'USER_INFO',
payload: userInfo
}
ws.send(JSON.stringify(wsAction));
broadcast({
type: 'USER_CONNECTED',
payload: {
userId: userInfo.userId
}
})
})
.catch( err => console.log('Error when resolving the userId\n', err));
break;
case 'NAME_CHANGE':
dbPromise.then( db => updateUserName(db, data.payload.userId, data.payload.name))
wsAction = {
type: 'NAME_CHANGE',
payload: {
userId: data.payload.userId,
name: data.payload.name,
idNamePair: { [data.payload.userId]: data.payload.name }
}
}
broadcast(wsAction);
break;
case 'MESSAGE':
const dbMessage = {
rawMessage: data.payload.rawMessage,
htmlMessage: markdown.toHTML(data.payload.rawMessage),
timestamp: new Date().getTime(),
userId: data.payload.userId,
}
dbPromise.then( db => saveMessage(db, dbMessage));
// TODO LOOK FOR XSS!
const wsMessage = {
type: "MESSAGE",
payload: {
htmlMessage: dbMessage.htmlMessage,
userId: dbMessage.userId,
timestamp: dbMessage.timestamp
}
};
broadcast(wsMessage);
break;
default:
// do nothing
}
});
});
console.log('Serving ' + usedServerName + ' at: \'' + usedWsAddress + '\'\n' +
'\nWhen you stop the server, run "yarn kill" to stop the database\n');
function exitHandler() {
console.log('\n\n!! REMEMBER TO RUN "yarn kill" TO STOP THE DATABASE !!');
}
//process.on('exit', exitHandler);
process.on('SIGINT', exitHandler);
//process.on('uncaughtException', exitHandler);