서버/클라이언트 스크립트 코드 합쳐서 100줄이 안 되는 간단한 채팅 프로그램입니다.
npm install
npm start
open http://localhost:3000/
{
"name": "mobile_chat",
"version": "1.0.0",
"description": "Mobile chatting",
"main": "./app.js",
"dependencies": {
"express": ">= 2.5",
"socket.io": ">= 0.9.2"
},
"scripts": {
"start": "forever start -c coffee app.coffee",
"stop": "forever stop -c coffee app.coffee"
}
}
<<< app.coffee >>>
express = require 'express'
app = express.createServer()
app.configure ->
app.use express.static(__dirname + '/public')
app.get '/', (req, res) ->
res.sendfile(__dirname + '/views/index.html')
io = require('socket.io').listen(app)
io.sockets.on 'connection', (socket) ->
console.log '* CONNECT'
socket.on 'enter', (data) ->
username = data.username.trim()
if username
console.log "* ENTER: #{username}"
socket.username = username
socket.emit 'enter'
io.sockets.emit 'talk',
message: "[ENTER] #{username}"
socket.on 'leave', (data) ->
console.log "* LEAVE: #{socket.username}"
socket.broadcast.emit 'talk',
message: "[LEAVE] #{socket.username}"
delete socket.username
socket.on 'talk', (data) ->
console.log "* TALK: #{data.message}"
io.sockets.emit 'talk',
message: "#{socket.username}: #{data.message}"
socket.on 'disconnect', (data) ->
console.log "* DISCONNECT: #{socket?.username}"
if socket.username
socket.broadcast.emit 'talk',
message: "[DISCONNECT] #{socket.username}"
app.listen 3000
console.log "Express server listening on port #{app.address().port}"
<<< views/index.html >>>
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<meta charset="utf-8">
<link href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.css" rel="stylesheet" type="text/css" />
<link href="/css/stylesheet.less" rel="stylesheet" type="text/less" />
<script src="http://lesscss.org/js/less.js" type="text/javascript"></script>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js" type="text/javascript"></script>
<script src="/socket.io/socket.io.js" type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-1.7.1.js" type="text/javascript"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.js" type="text/javascript"></script>
<script src="/js/application.coffee" type="text/coffeescript"></script>
</head>
<body>
<section id="loading" data-role="page">
Loading...
</section>
<section id="home" data-role="page">
<header data-role="header">
<h1>Chat</h1>
</header>
<div data-role="content">
<p>Welcome!</p>
<form>
<input type="text" name="username" placeholder="Username" />
<input type="submit" value="Enter" />
</form>
</div>
</section>
<section id="chat" data-role="page">
<header data-role="header">
<a href="#home" data-role="button" data-icon="arrow-l" data-direction="reverse">Back</a>
<h1>Chat room</h1>
</header>
<div data-role="content">
<div id="chat-area">
----
</div>
<form>
<input type="text" name="message" placeholder="Input here" />
<input type="submit" value="Send" />
</form>
</div>
</section>
</body>
</html>
<<< public/css/stylesheet.less >>>
#chat-area {
height: 200px;
overflow-y: scroll;
word-wrap: break-word;
}
<<< public/js/application.coffee >>>
$ ->
socket = io.connect('http://localhost')
socket.on 'disconnect', (data) ->
$.mobile.changePage '#home',
transition: 'flip'
socket.on 'enter', (data) ->
$('#chat-area').text('')
$.mobile.changePage '#chat',
transition: 'flip'
socket.on 'talk', (data) ->
message = $('<p></p>').text(data.message)
$('#chat-area').append(message)
.scrollTop($('#chat-area').prop('scrollHeight'))
$('#home').on 'submit', 'form', ->
socket.emit 'enter',
username: $('#home').find('input[name=username]').val()
false
$('#chat').on 'submit', 'form', ->
socket.emit 'talk',
message: $('#chat').find('input[name=message]').val()
this.reset()
false
$('#chat').on 'click', 'a[href=#home]', ->
socket.emit 'leave'
$.mobile.changePage '#home',
transition: 'pop'
댓글 1개
13년 전
좋은 정보 고맙습니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7830 | 9년 전 | 367 | ||
| 7829 |
|
9년 전 | 546 | |
| 7828 | 9년 전 | 478 | ||
| 7827 | 9년 전 | 366 | ||
| 7826 | 9년 전 | 373 | ||
| 7825 | 9년 전 | 425 | ||
| 7824 | 9년 전 | 412 | ||
| 7823 | 9년 전 | 314 | ||
| 7822 | 9년 전 | 313 | ||
| 7821 | 9년 전 | 261 | ||
| 7820 | 9년 전 | 312 | ||
| 7819 |
|
9년 전 | 712 | |
| 7818 | 10년 전 | 334 | ||
| 7817 | 10년 전 | 440 | ||
| 7816 | 10년 전 | 352 | ||
| 7815 | 10년 전 | 552 | ||
| 7814 | 10년 전 | 372 | ||
| 7813 | 10년 전 | 311 | ||
| 7812 | 10년 전 | 338 | ||
| 7811 | 10년 전 | 358 | ||
| 7810 | 10년 전 | 482 | ||
| 7809 | 10년 전 | 429 | ||
| 7808 | 10년 전 | 281 | ||
| 7807 | 10년 전 | 343 | ||
| 7806 |
프로그래머7
|
10년 전 | 1300 | |
| 7805 | 10년 전 | 1209 | ||
| 7804 |
zahir1312
|
10년 전 | 739 | |
| 7803 |
|
10년 전 | 1337 | |
| 7802 | 10년 전 | 392 | ||
| 7801 | 10년 전 | 821 | ||
| 7800 | 10년 전 | 1035 | ||
| 7799 | 10년 전 | 495 | ||
| 7798 | 10년 전 | 442 | ||
| 7797 | 10년 전 | 442 | ||
| 7796 | 10년 전 | 297 | ||
| 7795 | 10년 전 | 448 | ||
| 7794 | 10년 전 | 471 | ||
| 7793 | 10년 전 | 989 | ||
| 7792 | 10년 전 | 398 | ||
| 7791 | 10년 전 | 479 | ||
| 7790 | 10년 전 | 448 | ||
| 7789 |
fbastore
|
10년 전 | 1384 | |
| 7788 | 10년 전 | 481 | ||
| 7787 | 10년 전 | 343 | ||
| 7786 | 10년 전 | 496 | ||
| 7785 | 10년 전 | 514 | ||
| 7784 | 10년 전 | 580 | ||
| 7783 | 10년 전 | 383 | ||
| 7782 | 10년 전 | 436 | ||
| 7781 | 10년 전 | 841 | ||
| 7780 | 10년 전 | 773 | ||
| 7779 | 10년 전 | 745 | ||
| 7778 | 10년 전 | 313 | ||
| 7777 | 10년 전 | 388 | ||
| 7776 | 10년 전 | 393 | ||
| 7775 | 10년 전 | 332 | ||
| 7774 | 10년 전 | 595 | ||
| 7773 | 10년 전 | 317 | ||
| 7772 | 10년 전 | 660 | ||
| 7771 | 10년 전 | 322 | ||
| 7770 | 10년 전 | 606 | ||
| 7769 | 10년 전 | 318 | ||
| 7768 | 10년 전 | 540 | ||
| 7767 | 10년 전 | 1114 | ||
| 7766 | 10년 전 | 438 | ||
| 7765 | 10년 전 | 462 | ||
| 7764 |
잘살아보자
|
10년 전 | 321 | |
| 7763 |
|
10년 전 | 1393 | |
| 7762 |
Tosea
|
10년 전 | 1014 | |
| 7761 | 10년 전 | 610 | ||
| 7760 |
잘살아보자
|
10년 전 | 584 | |
| 7759 |
잘살아보자
|
10년 전 | 401 | |
| 7758 |
잘살아보자
|
10년 전 | 521 | |
| 7757 | 10년 전 | 1171 | ||
| 7756 |
ITBANK
|
10년 전 | 1214 | |
| 7755 | 10년 전 | 1892 | ||
| 7754 | 10년 전 | 983 | ||
| 7753 | 10년 전 | 838 | ||
| 7752 | 10년 전 | 1338 | ||
| 7751 |
잘살아보자
|
10년 전 | 466 | |
| 7750 |
잘살아보자
|
10년 전 | 429 | |
| 7749 |
잘살아보자
|
10년 전 | 429 | |
| 7748 |
잘살아보자
|
10년 전 | 417 | |
| 7747 |
잘살아보자
|
10년 전 | 502 | |
| 7746 |
잘살아보자
|
10년 전 | 618 | |
| 7745 |
잘살아보자
|
10년 전 | 865 | |
| 7744 |
잘살아보자
|
10년 전 | 383 | |
| 7743 | 10년 전 | 910 | ||
| 7742 |
starbros
|
10년 전 | 781 | |
| 7741 |
잘살아보자
|
10년 전 | 593 | |
| 7740 |
잘살아보자
|
10년 전 | 469 | |
| 7739 |
잘살아보자
|
10년 전 | 428 | |
| 7738 |
잘살아보자
|
10년 전 | 483 | |
| 7737 |
잘살아보자
|
10년 전 | 436 | |
| 7736 |
잘살아보자
|
10년 전 | 462 | |
| 7735 |
잘살아보자
|
10년 전 | 796 | |
| 7734 |
잘살아보자
|
10년 전 | 388 | |
| 7733 |
잘살아보자
|
10년 전 | 484 | |
| 7732 |
잘살아보자
|
10년 전 | 639 | |
| 7731 |
잘살아보자
|
10년 전 | 564 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기