2018-04-23 20:42:52 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
import React from 'react';
|
2018-05-02 16:24:16 +00:00
|
|
|
import {Alert, Form, FormGroup, FormControl, Button} from 'react-bootstrap';
|
|
|
|
|
2018-04-23 20:42:52 +00:00
|
|
|
class Whisper extends React.Component {
|
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
this.state = {
|
|
|
|
listenTo: '',
|
|
|
|
channel: '',
|
|
|
|
message: '',
|
|
|
|
subscribedChannels: [],
|
|
|
|
messageList: [],
|
|
|
|
logs: []
|
|
|
|
};
|
|
|
|
}
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
handleChange (e, name) {
|
|
|
|
this.state[name] = e.target.value;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-08-15 17:15:40 +00:00
|
|
|
checkEnter(e, func) {
|
|
|
|
if (e.key !== 'Enter') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
func.apply(this, [e]);
|
|
|
|
}
|
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
sendMessage (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
EmbarkJS.Messages.sendMessage({topic: this.state.channel, data: this.state.message});
|
|
|
|
this.addToLog("EmbarkJS.Messages.sendMessage({topic: '" + this.state.channel + "', data: '" + this.state.message + "'})");
|
|
|
|
}
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
listenToChannel (e) {
|
|
|
|
e.preventDefault();
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
const subscribedChannels = this.state.subscribedChannels;
|
|
|
|
subscribedChannels.push(<span>Subscribed to <b>{this.state.listenTo}</b>. Now try sending a message</span>);
|
|
|
|
this.setState({
|
|
|
|
subscribedChannels
|
|
|
|
});
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
EmbarkJS.Messages.listenTo({topic: [this.state.listenTo]}, (error, message) => {
|
|
|
|
const messageList = this.state.messageList;
|
|
|
|
if (error) {
|
|
|
|
messageList.push(<span className="alert-danger">Error: {error}</span>);
|
|
|
|
} else {
|
|
|
|
messageList.push(<span>Channel: <b>{message.topic}</b> | Message: <b>{message.data}</b></span>);
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
messageList
|
|
|
|
});
|
|
|
|
});
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})");
|
|
|
|
}
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
addToLog (txt) {
|
|
|
|
this.state.logs.push(txt);
|
|
|
|
this.setState({logs: this.state.logs});
|
|
|
|
}
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
{
|
|
|
|
!this.props.enabled ?
|
|
|
|
<React.Fragment>
|
|
|
|
<Alert bsStyle="warning">The node you are using does not support Whisper</Alert>
|
|
|
|
<Alert bsStyle="warning">The node uses an unsupported version of Whisper</Alert>
|
|
|
|
</React.Fragment> : ''
|
|
|
|
}
|
|
|
|
<h3>Listen To channel</h3>
|
2018-08-15 17:15:40 +00:00
|
|
|
<Form inline onKeyDown={(e) => this.checkEnter(e, this.listenToChannel)}>
|
2018-05-02 16:24:16 +00:00
|
|
|
<FormGroup>
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
|
|
|
defaultValue={this.state.listenTo}
|
|
|
|
placeholder="channel"
|
|
|
|
onChange={e => this.handleChange(e, 'listenTo')}/>
|
|
|
|
<Button bsStyle="primary" onClick={(e) => this.listenToChannel(e)}>Start Listening</Button>
|
|
|
|
<div id="subscribeList">
|
|
|
|
{this.state.subscribedChannels.map((item, i) => <p key={i}>{item}</p>)}
|
|
|
|
</div>
|
|
|
|
<p>messages received:</p>
|
|
|
|
<div id="messagesList">
|
|
|
|
{this.state.messageList.map((item, i) => <p key={i}>{item}</p>)}
|
|
|
|
</div>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
<h3>Send Message</h3>
|
2018-08-15 17:15:40 +00:00
|
|
|
<Form inline onKeyDown={(e) => this.checkEnter(e, this.sendMessage)}>
|
2018-05-02 16:24:16 +00:00
|
|
|
<FormGroup>
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
|
|
|
defaultValue={this.state.channel}
|
|
|
|
placeholder="channel"
|
|
|
|
onChange={e => this.handleChange(e, 'channel')}/>
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
|
|
|
defaultValue={this.state.message}
|
|
|
|
placeholder="message"
|
|
|
|
onChange={e => this.handleChange(e, 'message')}/>
|
|
|
|
<Button bsStyle="primary" onClick={(e) => this.sendMessage(e)}>Send Message</Button>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-05-02 16:24:16 +00:00
|
|
|
<p>Javascript calls being made: </p>
|
|
|
|
<div className="logs">
|
|
|
|
<p>EmbarkJS.Messages.setProvider('whisper')</p>
|
|
|
|
{
|
|
|
|
this.state.logs.map((item, i) => <p key={i}>{item}</p>)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
2018-04-23 20:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Whisper;
|