-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
81 lines (67 loc) · 2.52 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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" const="text/html;charset=UTF-8" />
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.1.3/socket.io.min.js"></script>
<title>Simple Chat App</title>
</head>
<body>
<header>
<h1>test socket</h1>
</header>
<section>
<div id="change_username">
<input id="username" type="text" />
<button id="send_username" type="button">Change username</button>
</div>
</section>
<section id="chatroom">
<section id="feedback"></section>
</section>
<section id="input_zone">
<input id="message" class="vertical-align" type="text" />
<button id="send_message" class="vertical-align" type="button">Send</button>
</section>
<script>
$(function () {
//make connection
console.log('ok')
//dev env
// var socket = io.connect('http://localhost:3000/')
//prod env
var socket = io.connect('https://fstack2.herokuapp.com/')
socket.emit('getHistory', { username: 'testGetHistoryFromClientWeb' })
//buttons and inputs
var message = $("#message")
var username = $("#username")
var send_message = $("#send_message")
var send_username = $("#send_username")
var chatroom = $("#chatroom")
var feedback = $("#feedback")
//Emit message
send_message.click(function () {
socket.emit('priceAlertSubscribe', { message: message.val() })
})
//Listen on new_message
socket.on("new_message", (data) => {
feedback.html('');
message.val('');
chatroom.append("<p class='message'>" + data.username + ": " + data.message + "</p>")
})
//Emit a username
send_username.click(function () {
socket.emit('change_username', { username: username.val() })
})
//Emit typing
message.bind("keypress", () => {
socket.emit('typing')
})
//Listen on typing
socket.on('typing', (data) => {
feedback.html("<p><i>" + data.username + " is typing a message..." + "</i></p>")
})
});
</script>
</body>
</html>