index.ejs 2.32 KB
<% 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 %>