forked from ScaleDrone/react-chat-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
94 lines (86 loc) · 2.9 KB
/
App.js
File metadata and controls
94 lines (86 loc) · 2.9 KB
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
import React, { Component } from 'react';
import './App.css';
import Messages from "./Messages";
import Input from "./Input";
function randomName() {
const adjectives = [
"autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark",
"summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter",
"patient", "twilight", "dawn", "crimson", "wispy", "weathered", "blue",
"billowing", "broken", "cold", "damp", "falling", "frosty", "green", "long",
"late", "lingering", "bold", "little", "morning", "muddy", "old", "red",
"rough", "still", "small", "sparkling", "throbbing", "shy", "wandering",
"withered", "wild", "black", "young", "holy", "solitary", "fragrant",
"aged", "snowy", "proud", "floral", "restless", "divine", "polished",
"ancient", "purple", "lively", "nameless"
];
const nouns = [
"waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning",
"snow", "lake", "sunset", "pine", "shadow", "leaf", "dawn", "glitter",
"forest", "hill", "cloud", "meadow", "sun", "glade", "bird", "brook",
"butterfly", "bush", "dew", "dust", "field", "fire", "flower", "firefly",
"feather", "grass", "haze", "mountain", "night", "pond", "darkness",
"snowflake", "silence", "sound", "sky", "shape", "surf", "thunder",
"violet", "water", "wildflower", "wave", "water", "resonance", "sun",
"wood", "dream", "cherry", "tree", "fog", "frost", "voice", "paper", "frog",
"smoke", "star"
];
const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
const noun = nouns[Math.floor(Math.random() * nouns.length)];
return adjective + noun;
}
function randomColor() {
return '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16);
}
class App extends Component {
state = {
messages: [],
member: {
username: randomName(),
color: randomColor(),
}
}
constructor() {
super();
this.drone = new window.Scaledrone("YOUR-CHANNEL-ID", {
data: this.state.member
});
this.drone.on('open', error => {
if (error) {
return console.error(error);
}
const member = {...this.state.member};
member.id = this.drone.clientId;
this.setState({member});
});
const room = this.drone.subscribe("observable-room");
room.on('data', (data, member) => {
const messages = this.state.messages;
messages.push({member, text: data});
this.setState({messages});
});
}
render() {
return (
<div className="App">
<div className="App-header">
<h1>My Chat App</h1>
</div>
<Messages
messages={this.state.messages}
currentMember={this.state.member}
/>
<Input
onSendMessage={this.onSendMessage}
/>
</div>
);
}
onSendMessage = (message) => {
this.drone.publish({
room: "observable-room",
message
});
}
}
export default App;