-
Notifications
You must be signed in to change notification settings - Fork 3
/
websocket.js
139 lines (119 loc) · 3.4 KB
/
websocket.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
import { emitter, all, set } from './storage';
import diagnostics from 'diagnostics';
import Report from './report';
//
// Setup a debug instance.
//
const debug = diagnostics('anubis:websocket');
/**
* Interact with out WebSocket connection to handle communication between server
* and client.
*
* @param {WebSocket} client Incoming WebSocket connection.
* @private
*/
function incoming(boot) {
/**
* Handle incoming connections.
*
* @param {WebSocket} client Incoming WebSocket connection.
* @private
*/
return function connection(client) {
const report = new Report(boot);
const destiny = boot.get('destiny');
/**
* Process updates for the config.
*
* @param {String} key name of the config value that changed
* @param {Mixed} value New value
* @private
*/
function update(key, value) {
const payload = {};
payload[key] = value;
client.send(JSON.stringify({ type: 'config', payload: payload }), () => {
//
// Catch potential errors.
//
});
}
emitter.on('config', update);
all(function allstorage(err, data) {
client.send(JSON.stringify({ type: 'config', payload: data }), () => {
//
// Catch potential errors.
//
});
});
//
// All our supported RPC endpoints.
//
const endpoints = {
'config.set': function configchange(data, next) {
set(data.key, data.value, next);
},
'destiny.active.advisors': function advisors(data, next) {
destiny.go(function go() {
const active = destiny.characters.active();
if (active) active.advisors(function advisors(err, data) {
if (err) return next(err);
//console.log(JSON.stringify(data.activities.trials, null, 2));
data.type = 'advisors';
next(undefined, data);
});
});
},
'destiny.trials.report': function search(username, next) {
report.lookup(username, (failed) => {
next(failed);
});
}
};
report.on('fireteam', () => {
client.send(JSON.stringify({
type: 'report',
fireteam: report.fireteam
}));
});
report.on('loadout', () => {
client.send(JSON.stringify({
type: 'report',
loadout: report.loadout
}));
});
report.on('error', (err) => {
client.send(JSON.stringify({
type: 'report',
err: err
}));
});
//
// Handle incoming RPC messages from the client.
//
client.on('message', function incoming(message) {
let data;
try { data = JSON.parse(message); }
catch (e) { return debug('failed to parse message', e); }
if (data.type === 'rpc') {
if (data.endpoint in endpoints) {
return endpoints[data.endpoint](data.data, (...args) => {
client.send(JSON.stringify({ type: 'rpc', id: data.id, args: args }), () => {
//
// This flow is completely async, so it could be that by the time we
// got our data the connection was already closed. Hence this
// callback.
//
});
});
}
debug('unknown rpc(%s) endpoint, for id %s', data.endpoint, data.id);
}
});
client.on('close', function () {
emitter.off('config', update);
report.destroy();
});
}
}
export { incoming };