-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatApp.js
More file actions
35 lines (31 loc) · 861 Bytes
/
chatApp.js
File metadata and controls
35 lines (31 loc) · 861 Bytes
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
// var name = prompt("What's your name mate?");
var sock = new WebSocket("ws://localhost:5001");
var log = document.getElementById('log');
sock.onopen = function(){
sock.send(JSON.stringify({
type: "name",
data: name
}));
}
sock.onmessage = function(event){
// Display received message from the server
console.log(event);
var json = JSON.parse(event.data);
log.innerHTML += json.name + ":"+ json.data + "<br>";
}
sock.onerror = function(event) {
console.log(event);
}
sock.onclose = function(event){
console.log(event);
}
document.querySelector('button').onclick = function(){
var text = document.getElementById('text').value;
sock.send(JSON.stringify({
type: "message",
data: text
}));
document.getElementById('text').value = null;
// Display input message from user
log.innerHTML += "You: "+ text + "<br>";
};