-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
116 lines (82 loc) · 2.65 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>websockets</title>
</head>
<body>
<div class="kk">
<img id="logo" src="anonymouslogo2.png" width="50px" height="50px">
</div>
<div id='username' style="font-size: 30px;text-align: center;font-weight: bold;">
</div><br>
<div id="log"></div>
<div id="sendCtrls">
<input type="text" placeholder="Your message here" id="text" autofocus>
<button id="btn">send hello</button>
</div>
<script>
var name = prompt("What is your name fella ");
checkName(name);
function checkName(name)
{
if(name!="")
{
document.getElementById('username').innerHTML=name.toUpperCase();
}
else
{
var name = prompt("What is your name fella ");
if(name!=""){
document.getElementById('username').innerHTML=name.toUpperCase();
}
else{
checkName(name);
}
}
}
var sock = new WebSocket("ws://localhost:5001");
sock.onopen = function(){
sock.send(JSON.stringify({
type : "name",
data : document.getElementById('username').innerHTML
}));
}
var log = document.getElementById('log');
sock.onmessage = function(event){ //again get back the message from the server
console.log(event);
var json = JSON.parse(event.data);
if(json.data!=="")
{
log.innerHTML+= "<div id='blckfriend'><span id='friend'><b>"+json.name+
"</b><br></span><span id='friendText'> "+json.data+"</span></div>"; //showcase in div what we got from the server
}
}
document.querySelector('button').onclick = function(){
var timeANDdate=new Date().toLocaleString();
var text = document.getElementById('text').value;
//sock.send(text); //send text to the server
sock.send(JSON.stringify({
type : "message",
data : text+"<br><span class='date'>"+timeANDdate+"</span><br>"
}));
if(text!=="")
{
log.innerHTML+= "<div id='blckyou'><span id='you'><span><b>You</b><br></span> "+
"</span><span id='youText'>"+ text +"</span><br><span class='date'>"
+timeANDdate+"</span><br></div>";
document.getElementById('text').value="";
}
document.getElementById('log').scrollTop = 9999999; //shows latest chat scrolled
};
document.getElementById("text").addEventListener("keyup", function(event) {
//press enter and onclick or submit
if (event.keyCode === 13)
{
document.getElementById("btn").onclick();
return false;
}
});
</script>
</body>
</html>