-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
74 lines (68 loc) · 1.34 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
<!DOCTYPE>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Chat</title>
<style>
#chat{
width:700px;
height:400px;
border:1px solid #ddd;
overflow-y: scroll;
padding:5px;
}
#message{
width:400px;
}
#chat p{#
margin:0 0 0 5px;
}
.error{
color:red;
}
</style>
<script src="/jquery.js"></script>
</head>
<body>
<h2>Chat</h2>
<div id="chat"></div>
<input type="text" id="message">
<button id="send">Nachricht senden</button>
<script>
var msg = {
name:"",
message: ""
}
var ws = new WebSocket('ws://'+window.location.host);
(function setName(){
msg.name = prompt("Willkommen im Chat! Wie ist dein Name?", "");
if(msg.name == ""){
setName();
}
})();
$('#message').keydown(function(evt){
if(evt.which==13){
msg.message = $('#message').val();
ws.send(JSON.stringify(msg));
msg.message = "";
$('#message').val("");
}
});
$('#send').click(function(){
msg.message = $('#message').val();
ws.send(JSON.stringify(msg));
msg.message = "";
$('#message').val("");
});
ws.onmessage = function(evt){
try{
var message = JSON.parse(evt.data);
$('#chat').append('<p><b>'+message.name+'</b>: ' +message.message+'<p>');
}
catch(e){
$('#chat').append('<p class="error">invalid message</p>');
}
}
</script>
</body>
</html>