서버/클라이언트 스크립트 코드 합쳐서 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년 전
좋은 정보 고맙습니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5130 | 13년 전 | 750 | ||
| 5129 | 13년 전 | 1759 | ||
| 5128 | 13년 전 | 928 | ||
| 5127 | 13년 전 | 504 | ||
| 5126 | 13년 전 | 743 | ||
| 5125 | 13년 전 | 1350 | ||
| 5124 | 13년 전 | 818 | ||
| 5123 | 13년 전 | 455 | ||
| 5122 | 13년 전 | 993 | ||
| 5121 |
|
13년 전 | 1379 | |
| 5120 | 13년 전 | 496 | ||
| 5119 | 13년 전 | 472 | ||
| 5118 | 13년 전 | 401 | ||
| 5117 | 13년 전 | 714 | ||
| 5116 | 13년 전 | 450 | ||
| 5115 | 13년 전 | 904 | ||
| 5114 | 13년 전 | 937 | ||
| 5113 |
cookieyou
|
13년 전 | 1345 | |
| 5112 | 13년 전 | 528 | ||
| 5111 | 13년 전 | 819 | ||
| 5110 | 13년 전 | 4441 | ||
| 5109 | 13년 전 | 1061 | ||
| 5108 | 13년 전 | 403 | ||
| 5107 | 13년 전 | 476 | ||
| 5106 | 13년 전 | 631 | ||
| 5105 | 13년 전 | 1096 | ||
| 5104 | 13년 전 | 411 | ||
| 5103 |
프로프리랜서
|
13년 전 | 1341 | |
| 5102 | 13년 전 | 1583 | ||
| 5101 | 13년 전 | 457 | ||
| 5100 | 13년 전 | 367 | ||
| 5099 | 13년 전 | 519 | ||
| 5098 | 13년 전 | 2128 | ||
| 5097 | 13년 전 | 374 | ||
| 5096 | 13년 전 | 521 | ||
| 5095 | 13년 전 | 383 | ||
| 5094 | 13년 전 | 388 | ||
| 5093 | 13년 전 | 874 | ||
| 5092 | 13년 전 | 1052 | ||
| 5091 | 13년 전 | 406 | ||
| 5090 | 13년 전 | 505 | ||
| 5089 | 13년 전 | 949 | ||
| 5088 | 13년 전 | 402 | ||
| 5087 | 13년 전 | 1152 | ||
| 5086 | 13년 전 | 1477 | ||
| 5085 |
|
13년 전 | 3923 | |
| 5084 |
alexseo
|
13년 전 | 907 | |
| 5083 | 13년 전 | 1121 | ||
| 5082 | 13년 전 | 597 | ||
| 5081 | 13년 전 | 519 | ||
| 5080 | 13년 전 | 593 | ||
| 5079 | 13년 전 | 967 | ||
| 5078 | 13년 전 | 2178 | ||
| 5077 | 13년 전 | 665 | ||
| 5076 |
몰보는거야
|
13년 전 | 1687 | |
| 5075 |
|
13년 전 | 1764 | |
| 5074 | 13년 전 | 2005 | ||
| 5073 |
|
13년 전 | 763 | |
| 5072 |
프로프리랜서
|
13년 전 | 1036 | |
| 5071 |
프로프리랜서
|
13년 전 | 762 | |
| 5070 |
프로프리랜서
|
13년 전 | 873 | |
| 5069 |
뭐먹고살지ㅠ
|
13년 전 | 781 | |
| 5068 |
danielle
|
13년 전 | 643 | |
| 5067 | 13년 전 | 758 | ||
| 5066 | 13년 전 | 833 | ||
| 5065 | 13년 전 | 903 | ||
| 5064 | 13년 전 | 1715 | ||
| 5063 | 13년 전 | 430 | ||
| 5062 | 13년 전 | 801 | ||
| 5061 | 13년 전 | 575 | ||
| 5060 | 13년 전 | 379 | ||
| 5059 |
senseme
|
13년 전 | 836 | |
| 5058 | 13년 전 | 2844 | ||
| 5057 | 13년 전 | 1909 | ||
| 5056 | 13년 전 | 1480 | ||
| 5055 | 13년 전 | 437 | ||
| 5054 |
senseme
|
13년 전 | 1062 | |
| 5053 |
senseme
|
13년 전 | 1903 | |
| 5052 |
senseme
|
13년 전 | 1035 | |
| 5051 |
senseme
|
13년 전 | 1057 | |
| 5050 |
senseme
|
13년 전 | 796 | |
| 5049 |
senseme
|
13년 전 | 974 | |
| 5048 | 13년 전 | 1982 | ||
| 5047 | 13년 전 | 487 | ||
| 5046 |
우렁찬우렁이
|
13년 전 | 854 | |
| 5045 |
크라이스트
|
13년 전 | 2146 | |
| 5044 | 13년 전 | 959 | ||
| 5043 | 13년 전 | 525 | ||
| 5042 | 13년 전 | 965 | ||
| 5041 |
|
13년 전 | 804 | |
| 5040 |
스마일라니
|
13년 전 | 866 | |
| 5039 | 13년 전 | 1131 | ||
| 5038 | 13년 전 | 1921 | ||
| 5037 | 13년 전 | 1041 | ||
| 5036 |
cookieyou
|
13년 전 | 1397 | |
| 5035 | 13년 전 | 492 | ||
| 5034 | 13년 전 | 554 | ||
| 5033 | 13년 전 | 485 | ||
| 5032 | 13년 전 | 431 | ||
| 5031 | 13년 전 | 727 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기