import EmbarkJS from 'Embark/EmbarkJS'; import React from 'react'; import {Alert, Form, FormGroup, FormControl, Button} from 'react-bootstrap'; class Whisper extends React.Component { constructor (props) { super(props); this.state = { listenTo: '', channel: '', message: '', subscribedChannels: [], messageList: [], logs: [] }; } handleChange (e, name) { this.state[name] = e.target.value; this.setState(this.state); } 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 + "'})"); } listenToChannel (e) { e.preventDefault(); const subscribedChannels = this.state.subscribedChannels; subscribedChannels.push(Subscribed to {this.state.listenTo}. Now try sending a message); this.setState({ subscribedChannels }); EmbarkJS.Messages.listenTo({topic: [this.state.listenTo]}, (error, message) => { const messageList = this.state.messageList; if (error) { messageList.push(Error: {error}); } else { messageList.push(Channel: {message.topic} | Message: {message.data}); } this.setState({ messageList }); }); this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})"); } addToLog (txt) { this.state.logs.push(txt); this.setState({logs: this.state.logs}); } render () { return ( { !this.props.enabled ? The node you are using does not support Whisper The node uses an unsupported version of Whisper : '' } Listen To channel this.handleChange(e, 'listenTo')}/> this.listenToChannel(e)}>Start Listening {this.state.subscribedChannels.map((item, i) => {item})} messages received: {this.state.messageList.map((item, i) => {item})} Send Message this.handleChange(e, 'channel')}/> this.handleChange(e, 'message')}/> this.sendMessage(e)}>Send Message Javascript calls being made: EmbarkJS.Messages.setProvider('whisper') { this.state.logs.map((item, i) => {item}) } ); } } export default Whisper;
{item}
messages received:
Javascript calls being made:
EmbarkJS.Messages.setProvider('whisper')