forked from cloudmine/CloudMineSDK-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.html
More file actions
76 lines (65 loc) · 2.78 KB
/
Copy pathchat.html
File metadata and controls
76 lines (65 loc) · 2.78 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
<!DOCTYPE HTML>
<html>
<head>
<script src="vendor/jquery-1.8.2.min.js" language="javascript"></script>
<script src="../js/cloudmine.js" language="javascript"></script>
<script language="javascript">
$(document).ready(function() {
// There are more options that can be passed to the WebService constructor
// that will act as defaults for any future call made on the webservice object.
var webservice = new cloudmine.WebService({
appid: "327a12c8208b4297a06f8f5fb32cfecc",
apikey: "931732f754654d3c80521024eba8f8ec"
});
// Always cache jquery results, it's quite slow.
var messageField = $('#message');
var nameField = $('#name');
// Setup polling of the CloudMine servers to update the chat box.
function updateChat() {
// Call the CloudMine API to grab all the data from the server. In this case
// we want to get all the keys since they represent all the messages.
// Rather than concatenating a really long string, array joining is faster.
var chat = [];
webservice.get(null).on('success', function(response) {
// Basic string concatination to build the string
// to display to the user in the chat box.
for (var i in response) {
var value = response[i];
chat.push(value.name + ': ' + value.message);
}
// Show the assembled string in the chat box.
$('#chat').val(chat.join('\n'));
});
// Poll again after 10 seconds
setTimeout(updateChat, 10000);
}
messageField.on('keydown', function(event) {
// Only bind to the return/enter key being pressed.
if (event.which == 13) {
// Construct some sort of unique identifier
// (in this case, we are using the UNIX timestamp).
var key = Date.now();
// This is our data model.
// Basically just store the person's name and their message in a JSON object.
var data = {
name: nameField.val(),
message: messageField.val()
};
// Call the CloudMine API to actually push the data to the CM servers.
webservice.update(key, data).on('success', function() {
updateChat();
});
// Clear the message box.
messageField.val('');
}
});
// Initial poll of data for chat.
updateChat();
});
</script>
</head>
<body>
<textarea id="chat" cols="60" rows="20"></textarea>
<p>Name: <input type="text" id="name" /> Message: <input type="text" id="message" /></p>
</body>
</html>