This repository was archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (53 loc) · 1.95 KB
/
app.js
File metadata and controls
70 lines (53 loc) · 1.95 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
// WARNING: PUBNUB KEYS REQUIRED FOR EXAMPLE TO FUNCTION
const PUBLISH_KEY = '';
const SUBSCRIBE_KEY = '';
// just making sure you're paying attention
if (PUBLISH_KEY === '' || SUBSCRIBE_KEY === '') {
alert('You forgot to enter your keys');
}
let ChatEngine = ChatEngineCore.create({
publishKey: PUBLISH_KEY,
subscribeKey: SUBSCRIBE_KEY
});
const getUsername = () => {
const animals = ['zebra', 'goat', 'cow', 'pig', 'tiger', 'wolf', 'pony', 'antelope'];
return animals[Math.floor(Math.random() * animals.length)];
};
const getColor = () => {
const colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Teal"];
return colors[Math.floor(Math.random() * colors.length)];
};
const appendMessage = (username, text) => {
let message =
$(`<div class="list-group-item" />`)
.append($('<strong>').text(username + ': '))
.append($('<span>').text(text));
$('#log').append(message);
$("#log").animate({ scrollTop: $('#log').prop("scrollHeight") }, "slow");
};
let me = ChatEngine.connect(getUsername(), {color: getColor()});
ChatEngine.on('$.ready', (data) => {
let me = data.me;
let chat = new ChatEngine.Chat('new-chat');
chat.on('$.connected', (payload) => {
appendMessage(me.uuid , 'Connected to chat!');
});
chat.on('$.online.here', (payload) => {
appendMessage('Status', payload.user.uuid + ' is in the channel! Their color is ' + payload.user.state.color + '.');
});
chat.on('$.online.join', (payload) => {
appendMessage('Status', payload.user.uuid + ' has come online! Their color is ' + payload.user.state.color + '.');
});
chat.on('message', (payload) => {
appendMessage(payload.sender.uuid, payload.data.text);
});
$("#message").keypress(function(event) {
if (event.which == 13) {
chat.emit('message', {
text: $('#message').val()
});
$("#message").val('');
event.preventDefault();
}
});
});