From 06c29ef1d19d46121827c3bc111e6f84db49b750 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 3 Feb 2020 10:03:43 -0500 Subject: [PATCH] latest api changes to match the netflux-server refactor --- lib/api.js | 17 +++++++++++++---- lib/historyKeeper.js | 35 +++++++++++++++++------------------ 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/api.js b/lib/api.js index d633db59a..c6491d5db 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,7 +1,7 @@ /* jshint esversion: 6 */ const nThen = require("nthen"); const WebSocketServer = require('ws').Server; -const NetfluxSrv = require('chainpad-server/NetfluxWebsocketSrv'); +const NetfluxSrv = require('chainpad-server'); module.exports.create = function (config) { var rpc; @@ -48,10 +48,19 @@ module.exports.create = function (config) { store: config.store, log: log, }; - // XXX historyKeeper exports a `setConfig` method - var wsSrv = new WebSocketServer(wsConfig); var historyKeeper = HK.create(hkConfig); - NetfluxSrv.run(wsSrv, config, historyKeeper); + + 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); }); }; diff --git a/lib/historyKeeper.js b/lib/historyKeeper.js index f8d3c8a15..92eb84a5c 100644 --- a/lib/historyKeeper.js +++ b/lib/historyKeeper.js @@ -976,25 +976,24 @@ module.exports.create = function (cfg) { command(ctx, seq, user, parsed); }; - // XXX every one of these values is exported because - // netfluxWebsocketServer needs them to do some magic historyKeeper things - // we could have netflux emit events and let historyKeeper handle them instead return { id: HISTORY_KEEPER_ID, - - // XXX dropChannel allows netflux to clear historyKeeper's cache - // maybe it should emit a 'channel_dropped' event instead - // and let historyKeeper decide what to do - dropChannel: dropChannel, - - // XXX we don't need to export checkExpired if netflux allows it to be HK's responsibility - checkExpired: checkExpired, - - // XXX again, if netflux emitted events then historyKeeper could handle them itself - // and netflux wouldn't need to have historyKeeper-specific code - onDirectMessage: onDirectMessage, - - // XXX same - onChannelMessage: onChannelMessage, + channelMessage: function (ctx, channel, msgStruct) { + onChannelMessage(ctx, channel, msgStruct); + }, + channelClose: function (channelName) { + dropChannel(channelName); + }, + channelOpen: function (ctx, channelName, user) { + ctx.sendMsg(ctx, user, [ + 0, + HISTORY_KEEPER_ID, // ctx.historyKeeper.id + 'JOIN', + channelName + ]); + }, + directMessage: function (ctx, seq, user, json) { + onDirectMessage(ctx, seq, user, json); + }, }; };