서버/클라이언트 스크립트 코드 합쳐서 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년 전
좋은 정보 고맙습니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 71 | ||
| 8229 | 9년 전 | 68 | ||
| 8228 |
커네드커네드
|
9년 전 | 112 | |
| 8227 | 9년 전 | 124 | ||
| 8226 | 9년 전 | 161 | ||
| 8225 | 9년 전 | 149 | ||
| 8224 | 9년 전 | 147 | ||
| 8223 | 9년 전 | 113 | ||
| 8222 |
|
9년 전 | 185 | |
| 8221 | 9년 전 | 90 | ||
| 8220 | 9년 전 | 102 | ||
| 8219 | 9년 전 | 99 | ||
| 8218 | 9년 전 | 139 | ||
| 8217 |
star3840
|
9년 전 | 113 | |
| 8216 | 9년 전 | 160 | ||
| 8215 | 9년 전 | 111 | ||
| 8214 | 9년 전 | 225 | ||
| 8213 | 9년 전 | 158 | ||
| 8212 | 9년 전 | 82 | ||
| 8211 | 9년 전 | 242 | ||
| 8210 | 9년 전 | 244 | ||
| 8209 | 9년 전 | 334 | ||
| 8208 | 9년 전 | 217 | ||
| 8207 | 9년 전 | 225 | ||
| 8206 |
|
9년 전 | 186 | |
| 8205 | 9년 전 | 169 | ||
| 8204 | 9년 전 | 131 | ||
| 8203 | 9년 전 | 232 | ||
| 8202 | 9년 전 | 139 | ||
| 8201 | 9년 전 | 181 | ||
| 8200 | 9년 전 | 162 | ||
| 8199 | 9년 전 | 213 | ||
| 8198 | 9년 전 | 174 | ||
| 8197 | 9년 전 | 167 | ||
| 8196 | 9년 전 | 554 | ||
| 8195 | 9년 전 | 155 | ||
| 8194 | 9년 전 | 280 | ||
| 8193 | 9년 전 | 155 | ||
| 8192 | 9년 전 | 188 | ||
| 8191 | 9년 전 | 141 | ||
| 8190 | 9년 전 | 127 | ||
| 8189 | 9년 전 | 189 | ||
| 8188 | 9년 전 | 128 | ||
| 8187 | 9년 전 | 140 | ||
| 8186 | 9년 전 | 141 | ||
| 8185 | 9년 전 | 309 | ||
| 8184 | 9년 전 | 108 | ||
| 8183 | 9년 전 | 324 | ||
| 8182 | 9년 전 | 166 | ||
| 8181 | 9년 전 | 130 | ||
| 8180 | 9년 전 | 692 | ||
| 8179 | 9년 전 | 486 | ||
| 8178 | 9년 전 | 310 | ||
| 8177 |
kiplayer
|
9년 전 | 318 | |
| 8176 | 9년 전 | 351 | ||
| 8175 | 9년 전 | 219 | ||
| 8174 | 9년 전 | 243 | ||
| 8173 | 9년 전 | 342 | ||
| 8172 | 9년 전 | 194 | ||
| 8171 | 9년 전 | 184 | ||
| 8170 | 9년 전 | 297 | ||
| 8169 |
커네드커네드
|
9년 전 | 260 | |
| 8168 | 9년 전 | 320 | ||
| 8167 | 9년 전 | 320 | ||
| 8166 | 9년 전 | 237 | ||
| 8165 | 9년 전 | 165 | ||
| 8164 | 9년 전 | 302 | ||
| 8163 | 9년 전 | 284 | ||
| 8162 | 9년 전 | 298 | ||
| 8161 | 9년 전 | 297 | ||
| 8160 |
|
9년 전 | 489 | |
| 8159 | 9년 전 | 427 | ||
| 8158 | 9년 전 | 248 | ||
| 8157 | 9년 전 | 374 | ||
| 8156 | 9년 전 | 279 | ||
| 8155 | 9년 전 | 251 | ||
| 8154 |
00년생용띠
|
9년 전 | 599 | |
| 8153 | 9년 전 | 229 | ||
| 8152 |
|
9년 전 | 407 | |
| 8151 | 9년 전 | 408 | ||
| 8150 | 9년 전 | 498 | ||
| 8149 |
Jangfolk
|
9년 전 | 348 | |
| 8148 | 9년 전 | 169 | ||
| 8147 | 9년 전 | 372 | ||
| 8146 | 9년 전 | 434 | ||
| 8145 | 9년 전 | 379 | ||
| 8144 | 9년 전 | 345 | ||
| 8143 | 9년 전 | 192 | ||
| 8142 | 9년 전 | 425 | ||
| 8141 | 9년 전 | 380 | ||
| 8140 | 9년 전 | 928 | ||
| 8139 | 9년 전 | 260 | ||
| 8138 |
전갈자리남자
|
9년 전 | 387 | |
| 8137 | 9년 전 | 395 | ||
| 8136 | 9년 전 | 744 | ||
| 8135 |
|
9년 전 | 794 | |
| 8134 |
PlayPixel
|
9년 전 | 513 | |
| 8133 |
|
9년 전 | 433 | |
| 8132 | 9년 전 | 447 | ||
| 8131 | 9년 전 | 811 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기