-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
66 lines (66 loc) · 1.77 KB
/
index.php
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
<?php
$session = mt_rand(1,999);
?>
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="js/jquery.js" type="text/javascript"></script>
<style type="text/css">
* {margin:0;padding:0;box-sizing:border-box;font-family:arial,sans-serif;resize:none;}
html,body {width:100%;height:100%;}
#wrapper {position:relative;margin:auto;max-width:1000px;height:100%;}
#chat_output {position:absolute;top:0;left:0;padding:20px;width:100%;height:calc(100% - 100px);}
#chat_input {position:absolute;bottom:0;left:0;padding:10px;width:100%;height:100px;border:1px solid #ccc;}
</style>
</head>
<body>
<div id="wrapper">
<div id="chat_output"></div>
<textarea id="chat_input" placeholder="Deine Nachricht..."></textarea>
<script type="text/javascript">
jQuery(function($){
// Websocket
var websocket_server = new WebSocket("ws://localhost:8080/");
websocket_server.onopen = function(e) {
websocket_server.send(
JSON.stringify({
'type':'socket',
'user_id':<?php echo $session; ?>
})
);
};
websocket_server.onerror = function(e) {
// Errorhandling
}
websocket_server.onmessage = function(e)
{
var json = JSON.parse(e.data);
switch(json.type) {
case 'chat':
$('#chat_output').append(json.msg);
break;
}
}
// Events
$('#chat_input').on('keyup',function(e){
if(e.keyCode==13 && !e.shiftKey)
{
var chat_msg = $(this).val();
websocket_server.send(
JSON.stringify({
'type':'chat',
'user_id':<?php echo $session; ?>,
'chat_msg':chat_msg
})
);
$(this).val('');
}
});
});
</script>
</div>
</body>
</html>