image

  • フォト Amazonギフト券
    ※この時計の時刻は、閲覧しているパソコンのものであり、必ずしも正確な時間とは限りません

検索

最近のトラックバック

無料ブログはココログ

« 2011-12-24 | トップページ | 2011-12-30 »

2011-12-25

2011-12-25

socket.io の examples/chat/app.js も coffeescript にしてみた。

socket.io の examples/chat/app.js
    https://github.com/LearnBoost/socket.io/tree/master/examples/chat
も coffeescript にしてみた。

このサンプルでは stylus をつかっている。
stylus は便利そうだ。
  - http://kyosuke.tumblr.com/post/14003234226/stylus
  > Kyosuke hamalog (Sass と LESS 以外の選択肢 Stylus)

apl.coffeescript の中身を示す。

$ cat app.coffee
express = require 'express'
stylus = require 'stylus'
nib = require 'nib'
sio = require 'socket.io'

app = express.createServer()
app.configure ->
  compile = (str, path) ->
    stylus(str).set('filename', path).use nib()

  app.use stylus.middleware(
    src: __dirname + '/public'
    compile: compile
  )
  app.use express.static(__dirname + '/public')
  app.set 'views', __dirname + '/views'
  app.set 'view engine', 'jade'

app.get '/', (req, res) ->
  res.render 'index', layout: false

app.listen 300, ->
  addr = app.address()
  console.log "   app listening on http://#{addr.address}:#{addr.port}"

io = sio.listen(app)
nicknames = {}

io.sockets.on 'connection', (socket) ->
  socket.on 'user message', (msg) ->
    socket.broadcast.emit 'user message', socket.nickname, msg

  socket.on 'nickname', (nick, fn) ->
    if nicknames[nick]
      fn true
    else
      fn false
      nicknames[nick] = socket.nickname = nick
      socket.broadcast.emit 'announcement', nick + ' connected'
      io.sockets.emit 'nicknames', nicknames

  socket.on 'disconnect', ->
    return  unless socket.nickname
    delete nicknames[socket.nickname]

    socket.broadcast.emit 'announcement', socket.nickname + ' disconnected'
    socket.broadcast.emit 'nicknames', nicknames

coffeescript で unzip

js2coffee で https://github.com/springmeyer/node-zipfile/blob/master/bin/unzip.js を coffeescript にしてみた。
(変換結果に対して ヘルプのメッセージ変更、空行の追加をしてある。)

$ coffee unzip.coffee ~/work/Books/What_Is_Node/What_Is_Node_.epub
unzipping: META-INF/container.xml
unzipping: OEBPS/cover.html
unzipping: OEBPS/index.html
unzipping: OEBPS/ch01.html
unzipping: OEBPS/ch01s08.html
unzipping: OEBPS/LiberationSerif.otf
unzipping: OEBPS/ch01s02.html
unzipping: OEBPS/LiberationMono-Italic.otf
unzipping: OEBPS/ch01s04.html
unzipping: OEBPS/LiberationMono-BoldItalic.otf
unzipping: OEBPS/author_bios.html
unzipping: OEBPS/ch01s05.html
unzipping: OEBPS/oreilly_large.gif
unzipping: OEBPS/content.opf
unzipping: OEBPS/httpatomoreillycomsourceoreillyimages867093.png.jpg
unzipping: OEBPS/ch01s03.html
unzipping: OEBPS/LiberationMono-Bold.otf
unzipping: OEBPS/httpatomoreillycomsourceoreillyimages867095.png.jpg
unzipping: OEBPS/core.css
unzipping: OEBPS/httpatomoreillycomsourceoreillyimages867097.png.jpg
unzipping: OEBPS/httpatomoreillycomsourceoreillyimages867099.png.jpg
unzipping: OEBPS/httpatomoreillycomsourceoreillyimages867091.jpg
unzipping: OEBPS/toc.ncx
unzipping: OEBPS/ch01s09.html
unzipping: OEBPS/LiberationMono.otf
unzipping: OEBPS/ch01s07.html
unzipping: OEBPS/ch01s06.html

この epub ファイルは
  http://www.amazon.com/What-Is-Node-ebook/dp/B005ISQ7JC/
の無料の電子書籍。

$ cat unzip.coffee
zip = require("zipfile")
fs = require("fs")
path = require("path")
usage = "usage: coffeescript unzip.coffee <zipfile>"

file = process.argv[2]
unless file
  console.log usage
  process.exit 1

zf = new zip.ZipFile(file)
zf.names.forEach (name) ->
  uncompressed = path.join(".", name)
  dirname = path.dirname(uncompressed)
  fs.mkdir dirname, 0755, (err) ->
    throw err  if err and not err.code.match(/^EEXIST/)
    if path.extname(name)
      buffer = zf.readFileSync(name)
      fd = fs.openSync(uncompressed, "w")
      console.log "unzipping: " + name
      fs.writeSync fd, buffer, 0, buffer.length, null
      fs.closeSync fd

« 2011-12-24 | トップページ | 2011-12-30 »

mokuji

2013年12月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

google

  • twitter
  • __
  • _
    Googleボットチェッカー

合わせて読む

  • 合わせて読む
    フィードメーター - katoy: cocolog あわせて読みたい

リンク