index.ejs
2.32 KB
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
<% include ../includes/header.ejs %>
<div class="row">
<div class="col-sm-10">
<div class="panel panel-default" id="chatWrap">
<div class="panel-heading">대화내용</div>
<div class="panel-body">
<ul id="chatBody"></ul>
</div>
</div>
</div>
<div class="col-sm-2">
<div class="panel panel-default" id="userWrap">
<div class="panel-heading">User</div>
<div class="panel-body">
<ul id="userList"></ul>
</div>
</div>
</div>
</div>
<div>
<form action="" method="post" id="sendForm">
<div class="input-group">
<input type="hidden" name="socketId">
<input type="text" name="message" class="form-control" placeholder="대화내용을 입력해주세요.">
<span class="input-group-btn">
<button class="btn btn-primary">작성하기</button>
</span>
</div><!-- /input-group -->
</form>
</div>
<style type="text/css">
.panel-default ul { padding-left:0px; }
.panel-default ul li { list-style:none; padding-left:0px;}
.panel-default .panel-body {min-height:350px; max-height:350px; overflow-y:scroll; }
#chatWrap ul li strong::after { content: " : "; }
@media (max-width: 768px) {
#userWrap { display:none; }
#chatWrap .panel-body { min-height:250px; }
}
</style>
<script src="/socket.io/socket.io.js"></script>
<script>
(function(){
var socket = io();
function updateUserList(userList){
$('#userList').html("");
for(var key in userList){
$('#userList').append('<li>' + userList[key] + '</li>');
}
}
socket.on('join', function(data){
updateUserList(data);
});
socket.on('server message', function(data){
$('#chatBody').append('<li><strong>'+ data.displayname +'</strong>' + data.message + '</li>');
});
socket.on('leave', function(data){
updateUserList(data);
});
$(document).ready(function() {
$('#sendForm').submit(function(){
var $massage = $('#sendForm input[name=message]');
socket.emit('client message', { message : $massage.val()});
$massage.val('');
return false;
});
});
})();
</script>
<% include ../includes/footer.ejs %>