서버/클라이언트 스크립트 코드 합쳐서 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년 전
좋은 정보 고맙습니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7930 | 9년 전 | 476 | ||
| 7929 | 9년 전 | 410 | ||
| 7928 | 9년 전 | 486 | ||
| 7927 | 9년 전 | 390 | ||
| 7926 | 9년 전 | 698 | ||
| 7925 | 9년 전 | 423 | ||
| 7924 | 9년 전 | 406 | ||
| 7923 | 9년 전 | 391 | ||
| 7922 | 9년 전 | 421 | ||
| 7921 | 9년 전 | 435 | ||
| 7920 | 9년 전 | 345 | ||
| 7919 | 9년 전 | 356 | ||
| 7918 | 9년 전 | 510 | ||
| 7917 | 9년 전 | 363 | ||
| 7916 | 9년 전 | 453 | ||
| 7915 | 9년 전 | 469 | ||
| 7914 | 9년 전 | 477 | ||
| 7913 | 9년 전 | 655 | ||
| 7912 | 9년 전 | 490 | ||
| 7911 | 9년 전 | 410 | ||
| 7910 | 9년 전 | 461 | ||
| 7909 | 9년 전 | 575 | ||
| 7908 | 9년 전 | 512 | ||
| 7907 | 9년 전 | 449 | ||
| 7906 | 9년 전 | 469 | ||
| 7905 | 9년 전 | 434 | ||
| 7904 | 9년 전 | 422 | ||
| 7903 | 9년 전 | 434 | ||
| 7902 | 9년 전 | 608 | ||
| 7901 |
|
9년 전 | 773 | |
| 7900 | 9년 전 | 666 | ||
| 7899 | 9년 전 | 450 | ||
| 7898 | 9년 전 | 449 | ||
| 7897 | 9년 전 | 408 | ||
| 7896 | 9년 전 | 423 | ||
| 7895 | 9년 전 | 542 | ||
| 7894 | 9년 전 | 448 | ||
| 7893 | 9년 전 | 426 | ||
| 7892 | 9년 전 | 462 | ||
| 7891 | 9년 전 | 818 | ||
| 7890 | 9년 전 | 1238 | ||
| 7889 | 9년 전 | 772 | ||
| 7888 |
limsy1987
|
9년 전 | 592 | |
| 7887 | 9년 전 | 638 | ||
| 7886 | 9년 전 | 524 | ||
| 7885 | 9년 전 | 494 | ||
| 7884 | 9년 전 | 486 | ||
| 7883 | 9년 전 | 494 | ||
| 7882 | 9년 전 | 552 | ||
| 7881 | 9년 전 | 530 | ||
| 7880 | 9년 전 | 646 | ||
| 7879 | 9년 전 | 531 | ||
| 7878 | 9년 전 | 1303 | ||
| 7877 | 9년 전 | 826 | ||
| 7876 | 9년 전 | 568 | ||
| 7875 | 9년 전 | 640 | ||
| 7874 |
|
9년 전 | 835 | |
| 7873 | 9년 전 | 564 | ||
| 7872 | 9년 전 | 732 | ||
| 7871 | 9년 전 | 546 | ||
| 7870 | 10년 전 | 667 | ||
| 7869 | 10년 전 | 482 | ||
| 7868 | 10년 전 | 523 | ||
| 7867 | 10년 전 | 529 | ||
| 7866 | 10년 전 | 586 | ||
| 7865 | 10년 전 | 530 | ||
| 7864 | 10년 전 | 585 | ||
| 7863 | 10년 전 | 579 | ||
| 7862 | 10년 전 | 547 | ||
| 7861 | 10년 전 | 717 | ||
| 7860 | 10년 전 | 697 | ||
| 7859 | 10년 전 | 465 | ||
| 7858 | 10년 전 | 776 | ||
| 7857 | 10년 전 | 1162 | ||
| 7856 | 10년 전 | 585 | ||
| 7855 | 10년 전 | 824 | ||
| 7854 | 10년 전 | 758 | ||
| 7853 | 10년 전 | 663 | ||
| 7852 | 10년 전 | 584 | ||
| 7851 | 10년 전 | 586 | ||
| 7850 | 10년 전 | 657 | ||
| 7849 | 10년 전 | 426 | ||
| 7848 | 10년 전 | 494 | ||
| 7847 | 10년 전 | 732 | ||
| 7846 | 10년 전 | 516 | ||
| 7845 | 10년 전 | 493 | ||
| 7844 | 10년 전 | 457 | ||
| 7843 | 10년 전 | 493 | ||
| 7842 | 10년 전 | 478 | ||
| 7841 | 10년 전 | 447 | ||
| 7840 | 10년 전 | 463 | ||
| 7839 | 10년 전 | 517 | ||
| 7838 | 10년 전 | 578 | ||
| 7837 | 10년 전 | 406 | ||
| 7836 | 10년 전 | 452 | ||
| 7835 | 10년 전 | 537 | ||
| 7834 |
|
10년 전 | 1232 | |
| 7833 | 10년 전 | 499 | ||
| 7832 | 10년 전 | 477 | ||
| 7831 | 10년 전 | 641 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기