2012-01-11 5 views
2

Windows上でnode.jsを使用するローカルアプリケーションを作成しています。そのアイデアは、特定のファイルを見て、ファイルが変更されたときにその内容をブラウザに送信することです。Node.js fs.watch - 単一変更のために複数回出力する

今、テストのために、私はコンソールからファイルのfs.watchを見ています。私が気づいたことは、私が見ているファイルへの単一の変更がコンソールに二重出力を生成するということです。ここ

がサーバである:fs.watchはコンソールで二重の出力を作成している理由について

<!doctype html> 
<html> 
    <head> 
    <title>Socket.IO demo</title> 
    </head> 
    <body> 
    <h1>Socket.IO demo</h1> 
    <input type="text" autofocus="autofocus" /> 
    <button type="button">publish</button> 
    <button type="button">broadcast</button> 
    <button type="button">whisper</button> 
    <p>Status: <span id="status">Undefined</span></p> 
    <ol id="messages"></ol> 
    <script src="/socket.io/socket.io.js"></script> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script> 
    <script type="text/coffeescript"> 

     jQuery ($) -> 

     $status = $ '#status' 
     socket = io.connect() 

     socket.on 'connect', -> 
      $status.text 'Connected' 

     socket.on 'disconnect', -> 
      $status.text 'Disconnected' 

     socket.on 'reconnecting', (seconds) -> 
      $status.text "Reconnecting in #{seconds} seconds" 

     socket.on 'reconnect', -> 
      $status.text 'Reconnected' 

     socket.on 'reconnect_failed', -> 
      $status.text 'Failed to reconnect' 

     socket.on 'message', (message) -> 
      $('<li>').text(message).appendTo $('#messages') 

     socket.on 'secret', (message) -> 
      console.log message 

     $input = $ 'input' 

     $('button').click -> 
      socket.emit $(this).text(), $input.val() 
      $input.val('').focus() 

    </script> 
    </body> 
</html> 

任意の考え:

var fs, http, io, server; 
fs = require('fs'); 
http = require('http'); 
server = http.createServer(function(req, res) { 
    return fs.readFile("" + __dirname + "/socket.io.demo.html", function(err, data) { 
    res.writeHead(200, { 
     'Content-Type': 'text/html' 
    }); 
    return res.end(data, 'utf8'); 
    }); 
}); 
server.listen(1337); 

fs.watch('/', function (event, filename) { 
    console.log('event is: ' + event); 
    if ('test.txt') { 
    console.log('filename provided: ' + filename); 
    } else { 
    console.log('filename not provided'); 
    } 
}); 

io = require('socket.io').listen(server); 
io.sockets.on('connection', function(socket) { 
    socket.on('publish', function(message) { 
    return io.sockets.send(message); 
    }); 
    socket.on('broadcast', function(message) { 
    return socket.broadcast.send(message); 
    }); 
    return socket.on('whisper', function(message) { 
    return socket.broadcast.emit('secret', message); 
    }); 
}); 

ここにHTMLがありますか?

答えて

4

ノードバグのようです。 https://github.com/joyent/node/issues/2126

一時的な解決策として、イベントを円滑にするためにsetTimeoutを使用することを検討することができます。

+0

ありがとうLogan。私はそのバグを見ませんでした。 – imallhere

関連する問題