This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
chat.html
97 lines (77 loc) · 2.78 KB
/
chat.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PubNub ChatEngine</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<style type="text/css">
* {
margin: 0px;
padding: 0px;
}
body {
margin: 30px auto;
}
</style>
</head>
<body>
<div class="container">
<div class="col-sm-6 offset-sm-3">
<div class="card">
<div class="card-header">
Chat
</div>
<div class="card-block">
<div class="log" id="chat-output"></div>
<div class="input-group">
<input type="text" class="form-control message" placeholder="Your Message..." id="chat-input" onkeypress="checkSubmit(event)">
<span class="input-group-btn"><button class="btn btn-primary" type="submit" onclick="sendChat()">Send</button></span>
</div>
</div>
</div>
</div>
<script src="../node_modules/chat-engine/dist/chat-engine.js" type="text/javascript"></script>
<script type="text/javascript">
const now = new Date().getTime();
const username = ['user', now].join('-');
const textInput = document.getElementById('chat-input');
const textOutput = document.getElementById('chat-output');
let sendChat = function() {}; // will be filled in when ChatEngine connects
const ChatEngine = ChatEngineCore.create({
publishKey: '',
subscribeKey: ''
}, {
globalChannel: 'chat-engine-demo-js',
debug: true
});
ChatEngine.onAny((a) => {
// console.log(a)
});
ChatEngine.connect(username, {
signedOnTime: now
});
ChatEngine.on('$.ready', (data) => {
data.me.direct.onAny((a) => {
console.log(a)
})
sendChat = function(e) {
ChatEngine.global.emit('message', {
text: textInput.value
});
textInput.value = '';
return false;
};
checkSubmit = function(e) {
if (e.keyCode == 13) {
sendChat();
}
}
ChatEngine.global.on('message', (payload) => {
let div = document.createElement("p");
div.innerHTML = payload.sender.uuid + ': ' + payload.data.text;
textOutput.appendChild(div);
});
});
</script>
</body>
</html>