fix old WS not open and show subscriptions list

This commit is contained in:
Jonathan Rainville 2018-08-07 13:47:29 -04:00 committed by Iuri Matias
parent 8249bc05ff
commit d9bc1774f1
5 changed files with 21 additions and 13 deletions

View File

@ -10,7 +10,6 @@ class Communication extends Component {
listenTo: '', listenTo: '',
channel: '', channel: '',
message: '', message: '',
subscribedChannels: [],
messageList: [] messageList: []
}; };
} }
@ -28,13 +27,6 @@ class Communication extends Component {
listenToChannel(e) { listenToChannel(e) {
e.preventDefault(); e.preventDefault();
const subscribedChannels = this.state.subscribedChannels;
subscribedChannels.push(this.state.listenTo);
this.setState({
subscribedChannels
});
this.props.listenToMessages(this.state.listenTo); this.props.listenToMessages(this.state.listenTo);
} }
@ -52,11 +44,11 @@ class Communication extends Component {
<Button color="primary" onClick={(e) => this.listenToChannel(e)}>Start Listening</Button> <Button color="primary" onClick={(e) => this.listenToChannel(e)}>Start Listening</Button>
</Form.FieldSet> </Form.FieldSet>
{this.state.subscribedChannels.length > 0 && {this.props.subscriptions && this.props.subscriptions.length > 0 &&
<div id="subscribeList"> <div id="subscribeList">
<h4>Subscribed channels:</h4> <h4>Subscribed channels:</h4>
<List> <List>
{this.state.subscribedChannels.map((item, i) => <List.Item key={i}>{item}</List.Item>)} {this.props.subscriptions.map((item, i) => <List.Item key={i}>{item}</List.Item>)}
</List> </List>
</div> </div>
} }
@ -107,6 +99,7 @@ class Communication extends Component {
Communication.propTypes = { Communication.propTypes = {
sendMessage: PropTypes.func, sendMessage: PropTypes.func,
listenToMessages: PropTypes.func, listenToMessages: PropTypes.func,
subscriptions: PropTypes.array,
channels: PropTypes.object channels: PropTypes.object
}; };

View File

@ -34,7 +34,8 @@ class CommunicationContainer extends Component {
{isEnabledMessage} {isEnabledMessage}
<Communication listenToMessages={(channel) => this.listenToChannel(channel)} <Communication listenToMessages={(channel) => this.listenToChannel(channel)}
sendMessage={(channel, message) => this.sendMessage(channel, message)} sendMessage={(channel, message) => this.sendMessage(channel, message)}
channels={this.props.channels}/> channels={this.props.channels}
subscriptions={this.props.subscriptions}/>
</Page.Content> </Page.Content>
); );
} }
@ -45,12 +46,14 @@ CommunicationContainer.propTypes = {
messageListen: PropTypes.func, messageListen: PropTypes.func,
communicationVersion: PropTypes.func, communicationVersion: PropTypes.func,
channels: PropTypes.object, channels: PropTypes.object,
subscriptions: PropTypes.array,
version: PropTypes.number version: PropTypes.number
}; };
function mapStateToProps(state) { function mapStateToProps(state) {
return { return {
channels: state.messages.channels, channels: state.messages.channels,
subscriptions: state.messages.subscriptions,
version: state.messages.version version: state.messages.version
}; };
} }

View File

@ -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]: { case actions.MESSAGE_VERSION[actions.SUCCESS]: {
return { return {
...state, ...state,

View File

@ -148,7 +148,7 @@ class Whisper {
'ws', 'ws',
'/embark-api/communication/listenTo/:topic', '/embark-api/communication/listenTo/:topic',
(ws, req) => { (ws, req) => {
listenTo({ self.webSocketsChannels[req.params.topic] = listenTo({
topic: req.params.topic, topic: req.params.topic,
messageEvents, messageEvents,
toHex: self.web3.utils.toHex, toHex: self.web3.utils.toHex,
@ -157,6 +157,9 @@ class Whisper {
symKeyID, symKeyID,
subscribe: self.web3.shh.subscribe subscribe: self.web3.shh.subscribe
}, (err, result) => { }, (err, result) => {
if (ws.readyState === ws.CLOSED) {
return;
}
if (err) { if (err) {
return ws.status(500).send(JSON.stringify({error: err})); return ws.status(500).send(JSON.stringify({error: err}));
} }

View File

@ -99,7 +99,8 @@ function listenTo(options, callback) {
return callback(null, data); return callback(null, data);
} }
promise.cb(payload, data, result); promise.cb(payload, data, result);
}); })
.catch(callback);
return promise; return promise;
} }