-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
86 lines (73 loc) · 1.67 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php require 'config.php'; ?>
<!doctype html>
<html>
<head>
<!-- https://github.com/Flynsarmy/PHPWebSocket-Chat.git -->
<meta charset="utf-8">
<style>
input, textarea {
border: 1px solid #CCC;
margin: 0px;
padding: 0px;
}
#body {
max-width: 800px;
margin: auto;
}
#log {
width: 100%;
height: 400px;
}
#message {
width: 100%;
line-height: 20px;
}
</style>
</head>
<body>
<div id="body">
<textarea id="log" name="log" readonly="readonly"></textarea>
<br>
<input type="text" id="message" name="message">
</div>
<script src="/jquery.min.js"></script>
<script src="/fancywebsocket.js"></script>
<script>
var Server;
function log(text) {
$log = $('#log');
//Add text to log
$log.append(($log.val()?"\n":'')+text);
//Autoscroll
$log[0].scrollTop = $log[0].scrollHeight - $log[0].clientHeight;
}
function send(text) {
Server.send('message', text);
}
$(document).ready(function() {
log('Connecting...');
Server = new FancyWebSocket('<?php echo 'ws://'.WS_HOST.':'.WS_PORT; ?>');
$('#message').keypress(function(e) {
if (e.keyCode == 13 && this.value) {
log( 'You: ' + this.value );
send( this.value );
$(this).val('');
}
});
//Let the user know we're connected
Server.bind('open', function() {
log( "Connected." );
});
//OH NOES! Disconnection occurred.
Server.bind('close', function( data ) {
log( "Disconnected." );
});
//Log any messages sent from server
Server.bind('message', function( payload ) {
log( payload );
});
Server.connect();
});
</script>
</body>
</html>