-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApi.js
More file actions
59 lines (49 loc) · 1.21 KB
/
Api.js
File metadata and controls
59 lines (49 loc) · 1.21 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
'use strict';
/**
* Created by victor on 17.9.1.
*/
module.exports = class Api {
constructor(config, app, io) {
this.config = config;
this.app = app;
this.io = io;
this.io.on('connection', this.setConnection.bind(this));
}
setConnection(socket) {
socket.emit('login', 'Successfully connected!');
socket.on('send', data => {
this.check(data).then(result => {
console.log(result, !!result);
if (!!result.event)
socket.emit(result.event, result.data)
});
});
socket.on('add-message', (data) => {
socket.emit('new message', {
username: socket.username,
message: data,
date: Date.now()
});
socket.broadcast.emit('new message', {
username: socket.username,
message: data,
date: Date.now()
});
});
}
check(data) {
return new Promise((resolve, reject) => {
this._processData(data).then(res => {
resolve(res);
});
})
}
_processData(data) {
return new Promise((resolve, reject) => {
// TODO: send data to controllers
data.status = 'processed';
let result = {event: 'send', data: data};
resolve(result);
});
}
};