-
Notifications
You must be signed in to change notification settings - Fork 3
/
chat.js
39 lines (33 loc) · 1018 Bytes
/
chat.js
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
$(function(){
//make connection
var socket = io.connect('http://localhost:5000')
//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('new_message', {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>")
})
});