-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
55 lines (45 loc) · 1.46 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
// PS! Replace this with your own channel ID
// If you use this channel ID your app will stop working in the near future
const CHANNEL_ID = 'S9zR90COZmbxn8iW';
const drone = new ScaleDrone(CHANNEL_ID);
drone.on('close', event => console.log('Connection was closed', event));
drone.on('error', error => console.error(error));
const room = drone.subscribe('temperature', {
historyCount: 1 // ask for the latest message from history
});
room.on('history_message', ({data}) => {
console.log(data);
updateTemperatureDOM(data.temperature);
});
room.on('data', data => {
console.log(data);
updateTemperatureDOM(data.temperature);
});
//------------- DOM STUFF
const DOM = {
updateButton: document.querySelector('button'),
temperatureLabel: document.querySelector('.temperature'),
body: document.body,
};
DOM.updateButton.addEventListener('click', sendMessage);
function sendMessage() {
// This data could be sent by your server instead
drone.publish({
room: 'temperature',
message: {
temperature: Math.round(Math.random() * 80 - 35)
},
});
}
function updateTemperatureDOM(temperature) {
DOM.temperatureLabel.textContent = `${temperature}°C`;
if (temperature > 40) {
DOM.body.style.backgroundColor = '#e8511d';
} else if (temperature > 10) {
DOM.body.style.backgroundColor = '#ffa018';
} else if (temperature > -10) {
DOM.body.style.backgroundColor = '#19b0ff';
} else {
DOM.body.style.backgroundColor = '#5b58c2';
}
}