-
Notifications
You must be signed in to change notification settings - Fork 9
/
chat.php
58 lines (47 loc) · 1.62 KB
/
chat.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
<?php
include_once 'template/header.php' ?>
<div id="chat" style="overflow: auto;">
<p><strong>@system</strong>: please wait, I try to connect to the server.</p>
</div>
<div class="navbar-fixed-bottom">
<form id="form" action="">
<input id="input" type="text" class="form-control" placeholder="Text input" style="width: 100%;"
maxlength="140" autocomplete="off">
</form>
</div>
<!-- WebScokets -->
<script>
$(function () {
let ws;
let $chat = $('#chat');
let $form = $('#form');
let $input = $('#input');
function wsConnect() {
ws = new WebSocket("ws://seven.php.nixdev.co:1000/");
ws.onopen = function () {
msg("connection is open");
};
ws.onclose = function () {
msg("the connection is closed, I try to reconnect");
setTimeout(wsConnect, 1000);
};
ws.onmessage = function (evt) {
msg(evt.data);
$chat.scrollTop($chat[0].scrollHeight);
};
}
wsConnect();
$form.submit(function () {
ws.send($input.val());
$input.val('');
return false;
});
$chat.height($(window).height() - 80);
$input.focus();
function msg(message) {
$chat.append("<p>" + message + "</p>");
}
});
</script>
<?php
include_once 'template/footer.php' ?>