stop relying on netflux-server internals
* create RPC module from inside historyKeeper * stop passing around netflux-server context * update to use newer netflux-server's formal APIs * manage your own cache of indexes instead of storing things in the netflux context
This commit is contained in:
66
lib/api.js
66
lib/api.js
@@ -4,8 +4,6 @@ const WebSocketServer = require('ws').Server;
|
||||
const NetfluxSrv = require('chainpad-server');
|
||||
|
||||
module.exports.create = function (config) {
|
||||
var rpc;
|
||||
const log = config.log;
|
||||
const wsConfig = {
|
||||
server: config.httpServer,
|
||||
};
|
||||
@@ -32,35 +30,45 @@ module.exports.create = function (config) {
|
||||
});
|
||||
}, 1000 * 60 * 5); // run every five minutes
|
||||
}));
|
||||
}).nThen(function (w) {
|
||||
require("./rpc").create(config, w(function (e, _rpc) {
|
||||
if (e) {
|
||||
w.abort();
|
||||
throw e;
|
||||
}
|
||||
rpc = _rpc;
|
||||
}));
|
||||
}).nThen(function () {
|
||||
var HK = require('./historyKeeper.js');
|
||||
var hkConfig = {
|
||||
tasks: config.tasks,
|
||||
rpc: rpc,
|
||||
store: config.store,
|
||||
log: log,
|
||||
};
|
||||
// asynchronously create a historyKeeper and RPC together
|
||||
require('./historyKeeper.js').create(config, function (err, historyKeeper) {
|
||||
if (err) { throw err; }
|
||||
|
||||
var historyKeeper = HK.create(hkConfig);
|
||||
var log = config.log;
|
||||
|
||||
NetfluxSrv.create(new WebSocketServer(wsConfig))
|
||||
.on('channelClose', historyKeeper.channelClose)
|
||||
.on('channelMessage', historyKeeper.channelMessage)
|
||||
.on('channelOpen', historyKeeper.channelOpen)
|
||||
.on('sessionClose', function (userId, reason) {
|
||||
reason = reason; // XXX
|
||||
})
|
||||
.on('error', function (error, label, info) {
|
||||
info = info; // XXX
|
||||
})
|
||||
.register(historyKeeper.id, historyKeeper.directMessage);
|
||||
// spawn ws server and attach netflux event handlers
|
||||
NetfluxSrv.create(new WebSocketServer(wsConfig))
|
||||
.on('channelClose', historyKeeper.channelClose)
|
||||
.on('channelMessage', historyKeeper.channelMessage)
|
||||
.on('channelOpen', historyKeeper.channelOpen)
|
||||
.on('sessionClose', function (userId, reason) {
|
||||
if (['BAD_MESSAGE', 'SOCKET_ERROR', 'SEND_MESSAGE_FAIL_2'].indexOf(reason) !== -1) {
|
||||
return void log.error('SESSION_CLOSE_WITH_ERROR', {
|
||||
userId: userId,
|
||||
reason: reason,
|
||||
});
|
||||
}
|
||||
log.verbose('SESSION_CLOSE_ROUTINE', {
|
||||
userId: userId,
|
||||
reason: reason,
|
||||
});
|
||||
})
|
||||
.on('error', function (error, label, info) {
|
||||
if (!error) { return; }
|
||||
/* labels:
|
||||
SEND_MESSAGE_FAIL, SEND_MESSAGE_FAIL_2, FAIL_TO_DISCONNECT,
|
||||
FAIL_TO_TERMINATE, HANDLE_CHANNEL_LEAVE, NETFLUX_BAD_MESSAGE,
|
||||
NETFLUX_WEBSOCKET_ERROR
|
||||
*/
|
||||
log.error(label, {
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
info: info,
|
||||
});
|
||||
})
|
||||
.register(historyKeeper.id, historyKeeper.directMessage);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user