-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
95 lines (81 loc) · 2.38 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var mqtt = require('mqtt');
var React = require('react');
var host = 'ws://' + window.location.host + ':1884';
var client = mqtt.connect(host);
client.on('connect', function() {
client.subscribe("haw");
});
var Message = React.createClass({
render: function() {
return (
<div key={this.props.key} className="message">{this.props.message.payload}</div>
);
}
});
var MessageList = React.createClass({
addMessage: function(message) {
var updated = this.state.messages;
updated.push(message);
this.setState({messages: updated});
},
getInitialState: function() {
return { messages: [] };
},
componentDidMount: function() {
var self = this;
client.on('message', function(topic, payload, packet) {
self.addMessage({
key: Date.now(),
topic: topic,
payload: payload.toString()
});
});
},
render: function() {
var messageNodes = this.state.messages.map(function(message) {
return (
<Message key={message.key} message={message} />
);
});
return (
<div id="messageList" className="table-block footer-push">
<h1>Channel Name</h1>
<div className="messages">{messageNodes}</div>
</div>
);
}
});
var SendMessage = React.createClass({
render: function() {
return (
<div className="table-block">
<div className="container">
<footer id="footer" className="twelve columns">
<form onSubmit={this.send}>
<input type="text" name="sendMessage" className="u-full-width" placeholder="Say hello" autoFocus />
</form>
</footer>
</div>
</div>
);
},
send: function(event) {
event && event.preventDefault();
var input = event.target.children[0];
client.publish('haw', input.value);
input.value = "";
}
});
var App = React.createClass({
render: function() {
return (
<div className="table-container">
<MessageList />
<SendMessage />
</div>
);
}
});
React.render(
<App />, document.getElementById('body')
);