-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.html
99 lines (94 loc) · 2.53 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
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
<script src="/socket.io/socket.io.js"></script>
<h1>채팅창</h1>
<button type="button" id="return" style="float: right" onclick="location.href='http://localhost:3000'">돌아가기</button>
<!-- chat contents will be written down below. -->
<div id="chatContent">
</div>
<input id="myChat" type="text">
<input type="submit" id="send" value="Send">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var socket = io.connect('http://localhost:3000', {
path: '/socket.io'
// transports: ['websocket']
});
// receiving a message
socket.on('msg', function (data) {
var msgLine = $('<div class="msgLine">');
var msgBox = $('<div class="msgBox">');
msgBox.append(data);
msgBox.css('display', 'inline-block');
msgLine.append(msgBox);
$('#chatContent').append(msgLine);
// auto scorll down when a user send something
chatContent.scrollTop = chatContent.scrollHeight;
});
// sending a message
$('#myChat').on("keyup", function () {
if (window.event.keyCode==13 && $(this).val()!="") {
var msgLine = $('<div class="msgLine">');
var msgBox = $('<div class="msgBox">');
msgBox.append($(this).val());
msgBox.css('display', 'inline-block');
msgLine.css('text-align', 'right');
msgLine.append(msgBox);
$('#chatContent').append(msgLine);
socket.emit('msg', $(this).val());
$(this).val("");
chatContent.scrollTop = chatContent.scrollHeight;
}
});
$("#send").click(function() {
if ($('#myChat').val()!="") {
var msgLine = $('<div class="msgLine">');
var msgBox = $('<div class="msgBox">');
msgBox.append($('#myChat').val());
msgBox.css('display', 'inline-block');
msgLine.css('text-align', 'right');
msgLine.append(msgBox);
$('#chatContent').append(msgLine);
socket.emit('msg', $('#myChat').val());
$('#myChat').val("");
chatContent.scrollTop = chatContent.scrollHeight;
}
});
</script>
<style>
* {
box-sizing: border-box;
}
.msgLine {
margin: 15px;
}
.msgBox {
border: 1px solid black;
background: rgb(255, 255, 255);
padding: 2px 5px;
border-radius: 10px;
}
#chatContent {
border: 1px solid rgb(0, 0, 0);
width: 100%;
height: 200px;
margin-bottom: 10px;
overflow-y: auto;
}
#myChat {
width: 100%;
}
#msg, #myChat {
width: 80%;
height: 32px;
border-radius: 8px;
}
#send {
width: 16%;
height: 34px;
border-radius: 50px;
background: black;
color: white;
}
#return {
padding: 10px 30px;
}
</style>