-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathscript.js
131 lines (111 loc) · 4.24 KB
/
script.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// PS! Replace this with your own channel ID
// If you use this channel ID your app will stop working in the future
const CLIENT_ID = '4cNswoNqM2wVFHPg';
const drone = new ScaleDrone(CLIENT_ID, {
data: { // Will be sent out as clientData via events
name: getRandomName(),
color: getRandomColor(),
},
});
let members = [];
drone.on('open', error => {
if (error) {
return console.error(error);
}
console.log('Successfully connected to Scaledrone');
const room = drone.subscribe('observable-room');
room.on('open', error => {
if (error) {
return console.error(error);
}
console.log('Successfully joined room');
});
room.on('members', m => {
members = m;
updateMembersDOM();
});
room.on('member_join', member => {
members.push(member);
updateMembersDOM();
});
room.on('member_leave', ({id}) => {
const index = members.findIndex(member => member.id === id);
members.splice(index, 1);
updateMembersDOM();
});
room.on('data', (text, member) => {
if (member) {
addMessageToListDOM(text, member);
} else {
// Message is from server
}
});
});
drone.on('close', event => {
console.log('Connection was closed', event);
});
drone.on('error', error => {
console.error(error);
});
function getRandomName() {
const adjs = ["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"];
return (
adjs[Math.floor(Math.random() * adjs.length)] +
"_" +
nouns[Math.floor(Math.random() * nouns.length)]
);
}
function getRandomColor() {
return '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16);
}
//------------- DOM STUFF
const DOM = {
membersCount: document.querySelector('.members-count'),
membersList: document.querySelector('.members-list'),
messages: document.querySelector('.messages'),
input: document.querySelector('.message-form__input'),
form: document.querySelector('.message-form'),
};
DOM.form.addEventListener('submit', sendMessage);
function sendMessage() {
const value = DOM.input.value;
if (value === '') {
return;
}
DOM.input.value = '';
drone.publish({
room: 'observable-room',
message: value,
});
}
function createMemberElement(member) {
const { name, color } = member.clientData;
const el = document.createElement('div');
el.appendChild(document.createTextNode(name));
el.className = 'member';
el.style.color = color;
return el;
}
function updateMembersDOM() {
DOM.membersCount.innerText = `${members.length} users in room:`;
DOM.membersList.innerHTML = '';
members.forEach(member =>
DOM.membersList.appendChild(createMemberElement(member))
);
}
function createMessageElement(text, member) {
const el = document.createElement('div');
el.appendChild(createMemberElement(member));
el.appendChild(document.createTextNode(text));
el.className = 'message';
return el;
}
function addMessageToListDOM(text, member) {
const el = DOM.messages;
const wasTop = el.scrollTop === el.scrollHeight - el.clientHeight;
el.appendChild(createMessageElement(text, member));
if (wasTop) {
el.scrollTop = el.scrollHeight - el.clientHeight;
}
}