서버/클라이언트 스크립트 코드 합쳐서 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년 전
좋은 정보 고맙습니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5030 | 13년 전 | 777 | ||
| 5029 | 13년 전 | 461 | ||
| 5028 | 13년 전 | 715 | ||
| 5027 | 13년 전 | 1303 | ||
| 5026 | 13년 전 | 537 | ||
| 5025 | 13년 전 | 1345 | ||
| 5024 | 13년 전 | 407 | ||
| 5023 | 13년 전 | 652 | ||
| 5022 | 13년 전 | 418 | ||
| 5021 | 13년 전 | 654 | ||
| 5020 | 13년 전 | 504 | ||
| 5019 | 13년 전 | 847 | ||
| 5018 | 13년 전 | 894 | ||
| 5017 | 13년 전 | 777 | ||
| 5016 | 13년 전 | 2622 | ||
| 5015 | 13년 전 | 408 | ||
| 5014 | 13년 전 | 667 | ||
| 5013 | 13년 전 | 573 | ||
| 5012 |
JacobJeon
|
13년 전 | 1061 | |
| 5011 | 13년 전 | 792 | ||
| 5010 | 13년 전 | 1434 | ||
| 5009 |
JacobJeon
|
13년 전 | 1784 | |
| 5008 | 13년 전 | 1134 | ||
| 5007 | 13년 전 | 3003 | ||
| 5006 |
|
13년 전 | 1280 | |
| 5005 | 13년 전 | 658 | ||
| 5004 | 13년 전 | 2081 | ||
| 5003 | 13년 전 | 2515 | ||
| 5002 | 13년 전 | 375 | ||
| 5001 | 13년 전 | 513 | ||
| 5000 | 13년 전 | 409 | ||
| 4999 | 13년 전 | 509 | ||
| 4998 | 13년 전 | 452 | ||
| 4997 | 13년 전 | 649 | ||
| 4996 | 13년 전 | 1897 | ||
| 4995 | 13년 전 | 375 | ||
| 4994 | 13년 전 | 374 | ||
| 4993 | 13년 전 | 515 | ||
| 4992 | 13년 전 | 481 | ||
| 4991 | 13년 전 | 375 | ||
| 4990 | 13년 전 | 690 | ||
| 4989 | 13년 전 | 631 | ||
| 4988 | 13년 전 | 384 | ||
| 4987 | 13년 전 | 401 | ||
| 4986 | 13년 전 | 1369 | ||
| 4985 | 13년 전 | 651 | ||
| 4984 | 13년 전 | 1031 | ||
| 4983 | 13년 전 | 404 | ||
| 4982 |
쥬드125
|
13년 전 | 1000 | |
| 4981 | 13년 전 | 529 | ||
| 4980 | 13년 전 | 395 | ||
| 4979 | 13년 전 | 662 | ||
| 4978 | 13년 전 | 2163 | ||
| 4977 |
atria
|
13년 전 | 841 | |
| 4976 | 13년 전 | 1514 | ||
| 4975 | 13년 전 | 424 | ||
| 4974 | 13년 전 | 449 | ||
| 4973 | 13년 전 | 700 | ||
| 4972 | 13년 전 | 455 | ||
| 4971 | 13년 전 | 1383 | ||
| 4970 | 13년 전 | 350 | ||
| 4969 | 13년 전 | 406 | ||
| 4968 | 13년 전 | 716 | ||
| 4967 | 13년 전 | 510 | ||
| 4966 | 13년 전 | 431 | ||
| 4965 | 13년 전 | 573 | ||
| 4964 | 13년 전 | 575 | ||
| 4963 | 13년 전 | 417 | ||
| 4962 | 13년 전 | 2231 | ||
| 4961 | 13년 전 | 1322 | ||
| 4960 | 13년 전 | 1217 | ||
| 4959 | 13년 전 | 414 | ||
| 4958 | 13년 전 | 377 | ||
| 4957 | 13년 전 | 460 | ||
| 4956 | 13년 전 | 695 | ||
| 4955 | 13년 전 | 490 | ||
| 4954 | 13년 전 | 1057 | ||
| 4953 | 13년 전 | 1153 | ||
| 4952 | 13년 전 | 738 | ||
| 4951 |
sjsmsrjfflauswnrsmse
|
13년 전 | 872 | |
| 4950 | 13년 전 | 1898 | ||
| 4949 | 13년 전 | 2215 | ||
| 4948 | 13년 전 | 5523 | ||
| 4947 | 13년 전 | 408 | ||
| 4946 | 13년 전 | 849 | ||
| 4945 | 13년 전 | 403 | ||
| 4944 | 13년 전 | 604 | ||
| 4943 |
마당쇠2000
|
13년 전 | 1255 | |
| 4942 |
cula100jak
|
13년 전 | 823 | |
| 4941 | 13년 전 | 472 | ||
| 4940 | 13년 전 | 569 | ||
| 4939 | 13년 전 | 399 | ||
| 4938 | 13년 전 | 543 | ||
| 4937 | 13년 전 | 1476 | ||
| 4936 |
techer
|
13년 전 | 1818 | |
| 4935 | 13년 전 | 705 | ||
| 4934 | 13년 전 | 933 | ||
| 4933 | 13년 전 | 2022 | ||
| 4932 |
kajama78
|
13년 전 | 939 | |
| 4931 | 13년 전 | 975 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기