-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.html
64 lines (55 loc) · 1.73 KB
/
simple.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.0.8.js"></script>
<meta charset="UTF-8">
<title>Simple Chat</title>
<link rel="stylesheet" href="layout.css"/>
<style type="text/css">
body, input, button { font-size: 100%; }
body {
font-size: 300%;
}
#history { border: 1px solid black;}
#history, #message { border: 1px solid black;}
</style>
</head>
<body>
<div class="fill vbox">
<div id="history" class="grow scroll"></div>
<input id="message"/>
</div>
<script type="text/javascript">
// a simple JQuery like function to get elements by ID
function $(id) { return document.getElementById(id); }
//configure pubnub
var pubnub = new PubNub({
subscribeKey:"sub-c-b35ac5f0-24d2-11e8-bb29-5a43d096f02f",
publishKey:"pub-c-713a0c0e-087b-4d58-8430-fec42c8d83ea"
});
//add listener for message events
pubnub.addListener({
message: function(evt) {
$('history').innerHTML += "<p>"+evt.message.text+"</p>"
$('history').scrollTop = $('history').scrollHeight;
}
});
//subscribe to channel
pubnub.subscribe({channels:['converser','converser-out']});
//send the message
$('message').addEventListener('keypress',function(evt) {
if(evt.keyCode == 13) { //enter key
pubnub.publish({
// channel:'chatbotter',
// channel:'chatbot-translate',
channel: 'converser',
message:{text: $('message').value}
});
$('message').value = '';
$('message').focus();
}
});
$('message').focus();
</script>
</body>
</html>