-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.html
More file actions
81 lines (71 loc) · 1.81 KB
/
index.html
File metadata and controls
81 lines (71 loc) · 1.81 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
<!DOCTYPE html>
<style>
* {
margin:0;
border: none;
outline: none;
font-family: 'Helvetica';
font-size: 20px;
font-weight: bold;
}
#chat {
padding: 0 5px;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
#messages {
overflow: auto;
}
.message {
margin: 1em 0;
}
</style>
<div id="chat">
<div id="messages"></div>
<input disabled placeholder="Connecting..." id="newmessage"></input>
<button id="send" type="button">Send</button>
</div>
<!-- Imports jQuery -->
<script src='//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'>
</script>
<!-- Imports goog.appengine.Channel. -->
<script src='/_ah/channel/jsapi'></script>
<script>
var messages = $('#messages');
var newmessage = $('#newmessage');
// Wire up the channel event handlers.
var channel = new goog.appengine.Channel('{{ token }}');
var handler = {
'onopen': function() {
// Let the chatting begin.
newmessage.attr('placeholder', 'Send a message');
newmessage.attr('disabled', false);
newmessage.focus();
},
'onmessage': function(m) {
// Blindly append the message to the end of the element.
messages.append('<p class="message">' + m.data + '</p>');
},
'onerror': function() {}, // No-op.
'onclose': function() {}, // No-op.
};
channel.open(handler);
// Enter-button handler, i.e. send a message.
newmessage.on('keypress', function(e) {
if (e.key === 'Enter' && newmessage.val() !== '') {
$.post('/send', {data: newmessage.val()});
newmessage.val('');
};
});
// Send-button handler, i.e. send a message.
var send = $('#send');
send.click(function() {
console.log('test');
});
// Force all focus to the input box.
newmessage.on('blur', function(e) {
newmessage.focus();
});
</script>