-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
116 lines (103 loc) · 3.3 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
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
107
108
109
110
111
112
113
114
115
116
<!doctype html>
<html>
<head>
<title>Chat With Us</title>
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700italic,700' rel='stylesheet' type='text/css'>
<link href='/css/styles.css' rel='stylesheet' type='text/css'>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<header>
<h1>Chat for Visitors</h1>
</header>
<div class="content">
<section id="status">
<p>
<span class="status-item">Operator is:</span>
<span id="operator-status">Unavailable</span>
</p>
<p>
<span class="status-item">Your position in queue:</span>
<span id="queue-status"></span>
</p>
</section>
<ul id="messages"></ul>
<form id="form-join">
<h2>Join Chat as a Visitor</h2>
<input type="text" placeholder="Your nickname" id="nickname" required>
<input type="submit" value="Join Chat">
</form>
<form id="form-chat">
<input type="text" placeholder="Your message" id="message" autocomplete="off">
<input type="submit" value="Send" id="message-send">
</form>
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
var socket = io();
var messages = $('#messages');
var statusPanel = $('#status');
var formChat = $('#form-chat');
var formJoin = $('#form-join');
var message = $('#message');
var nickname = $('#nickname');
var queueStatus = $('#queue-status');
// Hide chat window to start and focus on the join form
statusPanel.hide();
formChat.hide();
messages.hide();
nickname.focus();
$("form").submit(function(event) {
event.preventDefault();
});
formJoin.submit(function() {
var name = nickname.val() || 'guest user';
socket.emit('visitor-joined-chat', name);
formJoin.detach();
statusPanel.show();
messages.show();
message.focus();
});
formChat.submit(function() {
socket.emit('message-to-operator', message.val());
// Clear the form
message.val('');
});
socket.on('update-visitor-queue', function(position) {
queueStatus.text(position);
});
socket.on('update-operator-status', function(isAvailable) {
var operatorStatus = (isAvailable === true ? 'Available' : 'Unavailable');
$('#operator-status').text(operatorStatus);
if (isAvailable) {
formChat.show();
message.focus();
}
});
socket.on('message-to-operator', function(name, message) {
messages.append(
$('<li>').text(message).prepend($('<span>')
.addClass('sender-name').text(name))
);
});
socket.on('message-to-visitor', function(name, message) {
messages.append(
$('<li>').text(message).prepend($('<span>')
.addClass('sender-name').text(name))
);
});
socket.on('chat-ended', function(message) {
messages.append($('<li>').text(message).addClass('session-end'));
queueStatus.hide();
formChat.hide();
socket.disconnect(true);
});
window.onbeforeunload = function () {
return "Do you really want to exit the chat?";
};
</script>
</body>
</html>