-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.html
70 lines (64 loc) · 2.3 KB
/
chat.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
<!DOCTYPE html>
<html>
<head>
<title> chat </title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
</head>
<body>
<h3 style="text-align:center;">Users</h3>
<div id="users"></div>
<div class="container">
<form id="message-form">
<p> <label> socket Id</label>
<input class="form-control socketIdClass" readonly/>
<p>
<label>Username</label>
<input class="form-control" id="username" />
</p>
<p>
<label>Message</label>
<textarea class="form-control" id="message"></textarea>
</p>
<button class="btn btn-primary" type="submit">Send</button>
</form>
<div id="messages"></div>
</div>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var acessToken = localStorage.accessToken;;
if (typeof (Storage) !== "undefined") {
if(acessToken === null || acessToken === '' || acessToken === undefined){
window.location.replace("http://localhost:3000/");
}
var socket = io('http://localhost:3000');
socket.on('user.add', function (id) {
$(`.socketIdClass`).attr('id', `${id}`).val(`${id}`);
});
socket.on('user.remove', function (id) {
$(`#${id}`).remove();
});
socket.on('message.sent', function (data) {
$('#messages').prepend(`
<div>
<hr />
<div><strong>${data.username}</strong></div>
<p>${data.message}</p>
</div>
`);
});
$(function () {
$('#message-form').on('submit', function (e) {
e.preventDefault();
socket.emit('message.send', {
message: $('#message').val(),
username: $('#username').val()
});
});
});
} else {
alert('browser not supported')
}
</script>
</body>
</html>