-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.php
106 lines (96 loc) · 3.84 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
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
<?php
ob_start();
session_start();
require_once 'language.php';
$sessid = session_id();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="chat">
<div style ="display: flex; justify-content: center; margin-bottom: 20px;">
<input type="text" name="" placeholder="Введите псевдоним" class="input_username_chat" value = "User12345">
<button id = "change_username_button" name = "connect_to">Изменить</button>
</div>
<div class="chat-container">
<div class="chat-frame">
<div class="chat-fix" style="margin-top: 30px;">
<div class="chat-fix-your" style="margin-left: 120px;">
</div>
</div>
</div>
<div style ="display: flex; justify-content: center;">
<input type="text" name="" placeholder="Введите сообщение для чата" class="input_chat">
</div>
</div>
</div>
<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('body').ready(function() {
var timer = setTimeout(update_messages, 400);
var cout_message = 0;
$('.input_chat').keypress(function (e) {
if (e.which == 13) {
if($('.input_chat').val().trim() != '') {
put_message_to_server($('.input_username_chat').val(), $('.input_chat').val());
} else {
$('.input_chat').val('');
}
return false;
}
});
function update_messages() {
var session_id = /SESS\w*ID=([^;]+)/i.test(document.cookie) ? RegExp.$1 : false;
$.ajax({
type: "POST",
url: 'chat_update_message.php',
dataType: "json",
success: function(data){
$('.chat-fix').html('');
data.messages.forEach(function(item, i, arr) {
if(session_id == data.sessions[i]) {
$('.chat-fix').append('<div class="chat-fix-your" style="margin-left: 120px;"><div class="your-message-chat"><span style="font-family: GothamSSm Narrow Light;">You:</span><br>' + item + '</div></div>');
} else {
if(data.accesses[i] == 1) {
$('.chat-fix').append('<div class="message-chat" style = "background: #e56565"><span style="font-family: GothamSSm Narrow Light;">' + data.usernames[i] + '</span><br>' + item + '</div>');
} else {
$('.chat-fix').append('<div class="message-chat"><span style="font-family: GothamSSm Narrow Light;">' + data.usernames[i] + '</span><br>' + item + '</div>');
}
}
});
if(data.messages.length > cout_message) {
cout_message = data.messages.length;
$('.chat-frame').scrollTop($('.chat-frame').prop('scrollHeight'));
}
}
});
timer = setTimeout(update_messages, 400);
}
function put_message_to_server(user, mes) {
$.ajax({
type: "POST",
url: 'chat_put_message.php',
data: {
message: mes,
username: user,
id_channel: 1212
},
success: function(response) {
$('.chat-fix').append('<div class="chat-fix-your" style="margin-left: 120px;"><div class="your-message-chat"><span style="font-family: GothamSSm Narrow Light;">You:</span><br>' + $('.input_chat').val() + '</div></div>');
last_message = $('.input_chat').val();
$('.input_chat').val('');
$('.chat-frame').scrollTop($('.chat-frame').prop('scrollHeight'));
}
});
}
});
</script>
</body>
</html>
<?php
ob_end_flush();
?>