서버/클라이언트 스크립트 코드 합쳐서 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년 전
좋은 정보 고맙습니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8030 | 9년 전 | 399 | ||
| 8029 | 9년 전 | 325 | ||
| 8028 | 9년 전 | 285 | ||
| 8027 | 9년 전 | 295 | ||
| 8026 | 9년 전 | 367 | ||
| 8025 | 9년 전 | 404 | ||
| 8024 | 9년 전 | 376 | ||
| 8023 | 9년 전 | 414 | ||
| 8022 | 9년 전 | 331 | ||
| 8021 | 9년 전 | 349 | ||
| 8020 | 9년 전 | 342 | ||
| 8019 | 9년 전 | 360 | ||
| 8018 | 9년 전 | 463 | ||
| 8017 | 9년 전 | 551 | ||
| 8016 | 9년 전 | 363 | ||
| 8015 | 9년 전 | 408 | ||
| 8014 | 9년 전 | 340 | ||
| 8013 | 9년 전 | 258 | ||
| 8012 | 9년 전 | 262 | ||
| 8011 | 9년 전 | 464 | ||
| 8010 | 9년 전 | 317 | ||
| 8009 | 9년 전 | 330 | ||
| 8008 | 9년 전 | 301 | ||
| 8007 | 9년 전 | 452 | ||
| 8006 | 9년 전 | 488 | ||
| 8005 |
|
9년 전 | 984 | |
| 8004 | 9년 전 | 373 | ||
| 8003 | 9년 전 | 444 | ||
| 8002 | 9년 전 | 339 | ||
| 8001 |
|
9년 전 | 682 | |
| 8000 | 9년 전 | 441 | ||
| 7999 | 9년 전 | 398 | ||
| 7998 | 9년 전 | 452 | ||
| 7997 | 9년 전 | 325 | ||
| 7996 | 9년 전 | 558 | ||
| 7995 | 9년 전 | 492 | ||
| 7994 | 9년 전 | 372 | ||
| 7993 | 9년 전 | 433 | ||
| 7992 | 9년 전 | 533 | ||
| 7991 | 9년 전 | 279 | ||
| 7990 | 9년 전 | 308 | ||
| 7989 | 9년 전 | 322 | ||
| 7988 | 9년 전 | 751 | ||
| 7987 | 9년 전 | 452 | ||
| 7986 | 9년 전 | 450 | ||
| 7985 | 9년 전 | 530 | ||
| 7984 | 9년 전 | 446 | ||
| 7983 | 9년 전 | 689 | ||
| 7982 | 9년 전 | 549 | ||
| 7981 | 9년 전 | 503 | ||
| 7980 | 9년 전 | 525 | ||
| 7979 | 9년 전 | 515 | ||
| 7978 | 9년 전 | 482 | ||
| 7977 | 9년 전 | 418 | ||
| 7976 | 9년 전 | 878 | ||
| 7975 | 9년 전 | 391 | ||
| 7974 | 9년 전 | 425 | ||
| 7973 | 9년 전 | 624 | ||
| 7972 | 9년 전 | 406 | ||
| 7971 | 9년 전 | 475 | ||
| 7970 | 9년 전 | 324 | ||
| 7969 | 9년 전 | 562 | ||
| 7968 | 9년 전 | 407 | ||
| 7967 | 9년 전 | 392 | ||
| 7966 | 9년 전 | 403 | ||
| 7965 |
|
9년 전 | 1037 | |
| 7964 | 9년 전 | 424 | ||
| 7963 | 9년 전 | 432 | ||
| 7962 | 9년 전 | 424 | ||
| 7961 |
전갈자리남자
|
9년 전 | 523 | |
| 7960 | 9년 전 | 987 | ||
| 7959 | 9년 전 | 568 | ||
| 7958 | 9년 전 | 428 | ||
| 7957 | 9년 전 | 381 | ||
| 7956 | 9년 전 | 380 | ||
| 7955 | 9년 전 | 482 | ||
| 7954 | 9년 전 | 413 | ||
| 7953 | 9년 전 | 460 | ||
| 7952 | 9년 전 | 382 | ||
| 7951 | 9년 전 | 518 | ||
| 7950 | 9년 전 | 414 | ||
| 7949 | 9년 전 | 408 | ||
| 7948 | 9년 전 | 343 | ||
| 7947 | 9년 전 | 956 | ||
| 7946 | 9년 전 | 467 | ||
| 7945 | 9년 전 | 420 | ||
| 7944 | 9년 전 | 467 | ||
| 7943 | 9년 전 | 405 | ||
| 7942 | 9년 전 | 425 | ||
| 7941 | 9년 전 | 415 | ||
| 7940 | 9년 전 | 917 | ||
| 7939 | 9년 전 | 391 | ||
| 7938 | 9년 전 | 423 | ||
| 7937 | 9년 전 | 308 | ||
| 7936 | 9년 전 | 901 | ||
| 7935 | 9년 전 | 484 | ||
| 7934 | 9년 전 | 457 | ||
| 7933 | 9년 전 | 572 | ||
| 7932 | 9년 전 | 523 | ||
| 7931 | 9년 전 | 577 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기