From c2c00835f8906df2398857148a903144fcb5b684 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 7 Aug 2018 13:47:29 -0400 Subject: [PATCH] fix old WS not open and show subscriptions list --- embark-ui/src/components/Communication.js | 13 +++---------- embark-ui/src/containers/CommunicationContainer.js | 5 ++++- embark-ui/src/reducers/messagesReducer.js | 8 ++++++++ lib/modules/whisper/index.js | 5 ++++- lib/modules/whisper/js/communicationFunctions.js | 3 ++- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/embark-ui/src/components/Communication.js b/embark-ui/src/components/Communication.js index 27f974cd0..b81a36c3a 100644 --- a/embark-ui/src/components/Communication.js +++ b/embark-ui/src/components/Communication.js @@ -10,7 +10,6 @@ class Communication extends Component { listenTo: '', channel: '', message: '', - subscribedChannels: [], messageList: [] }; } @@ -28,13 +27,6 @@ class Communication extends Component { listenToChannel(e) { e.preventDefault(); - - const subscribedChannels = this.state.subscribedChannels; - subscribedChannels.push(this.state.listenTo); - this.setState({ - subscribedChannels - }); - this.props.listenToMessages(this.state.listenTo); } @@ -52,11 +44,11 @@ class Communication extends Component { - {this.state.subscribedChannels.length > 0 && + {this.props.subscriptions && this.props.subscriptions.length > 0 &&

Subscribed channels:

- {this.state.subscribedChannels.map((item, i) => {item})} + {this.props.subscriptions.map((item, i) => {item})}
} @@ -107,6 +99,7 @@ class Communication extends Component { Communication.propTypes = { sendMessage: PropTypes.func, listenToMessages: PropTypes.func, + subscriptions: PropTypes.array, channels: PropTypes.object }; diff --git a/embark-ui/src/containers/CommunicationContainer.js b/embark-ui/src/containers/CommunicationContainer.js index 6a1b2e5a1..e13e67009 100644 --- a/embark-ui/src/containers/CommunicationContainer.js +++ b/embark-ui/src/containers/CommunicationContainer.js @@ -34,7 +34,8 @@ class CommunicationContainer extends Component { {isEnabledMessage} this.listenToChannel(channel)} sendMessage={(channel, message) => this.sendMessage(channel, message)} - channels={this.props.channels}/> + channels={this.props.channels} + subscriptions={this.props.subscriptions}/> ); } @@ -45,12 +46,14 @@ CommunicationContainer.propTypes = { messageListen: PropTypes.func, communicationVersion: PropTypes.func, channels: PropTypes.object, + subscriptions: PropTypes.array, version: PropTypes.number }; function mapStateToProps(state) { return { channels: state.messages.channels, + subscriptions: state.messages.subscriptions, version: state.messages.version }; } diff --git a/embark-ui/src/reducers/messagesReducer.js b/embark-ui/src/reducers/messagesReducer.js index e3857ea98..dac9ef3d1 100644 --- a/embark-ui/src/reducers/messagesReducer.js +++ b/embark-ui/src/reducers/messagesReducer.js @@ -16,6 +16,14 @@ export default function messages(state = {channels: {}}, action) { } }; } + case actions.MESSAGE_LISTEN[actions.REQUEST]: { + const subscriptions = state.subscriptions || []; + subscriptions.push(action.channel); + return { + ...state, + subscriptions: subscriptions + }; + } case actions.MESSAGE_VERSION[actions.SUCCESS]: { return { ...state, diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 0ff15fc11..b18798072 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -166,7 +166,7 @@ class Whisper { 'ws', '/embark-api/communication/listenTo/:topic', (ws, req) => { - listenTo({ + self.webSocketsChannels[req.params.topic] = listenTo({ topic: req.params.topic, messageEvents, toHex: self.web3.utils.toHex, @@ -175,6 +175,9 @@ class Whisper { symKeyID, subscribe: self.web3.shh.subscribe }, (err, result) => { + if (ws.readyState === ws.CLOSED) { + return; + } if (err) { return ws.status(500).send(JSON.stringify({error: err})); } diff --git a/lib/modules/whisper/js/communicationFunctions.js b/lib/modules/whisper/js/communicationFunctions.js index 5f5a20997..c265c8954 100644 --- a/lib/modules/whisper/js/communicationFunctions.js +++ b/lib/modules/whisper/js/communicationFunctions.js @@ -99,7 +99,8 @@ function listenTo(options, callback) { return callback(null, data); } promise.cb(payload, data, result); - }); + }) + .catch(callback); return promise; }