node.js + CoffeeScript + Socket.IO + jQuery Mobile > 개발자팁

개발자팁

개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.

node.js + CoffeeScript + Socket.IO + jQuery Mobile 정보

node.js node.js + CoffeeScript + Socket.IO + jQuery Mobile

본문

서버/클라이언트 스크립트 코드 합쳐서 100줄이 안 되는 간단한 채팅 프로그램입니다.

npm install
npm start
open http://localhost:3000/


<<< package.json >>>
{
    "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
  • 복사

댓글 1개

© SIRSOFT
현재 페이지 제일 처음으로