1

Socket.io 似乎没有通过 connect 提供其 socket.io.js 文件。

这是我的 server.js 代码:

    var app = require('http').createServer(handler),
  io = require('socket.io').listen(app),
  xml2js = require('xml2js'),
  parser = new xml2js.Parser(),
  fs = require('fs');

// creating the server ( localhost:8000 )
app.listen(8080);


// on server started we can load our client.html page

function handler(req, res) {
      console.log('liccy');

  fs.readFile(__dirname + '/', function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(500);
      return res.end('Error loading client.html');
    }
    res.writeHead(200);
    res.end(data);
  });
}

// creating a new websocket to keep the content updated without any AJAX request
io.sockets.on('connection', function(socket) {
  console.log(__dirname);
  // watching the xml file
  fs.watch(__dirname + '/example.xml', function(curr, prev) {
    // on file change we can read the new xml
    fs.readFile(__dirname + '/example.xml', function(err, data) {
      if (err) throw err;
      // parsing the new xml data and converting them into json file
      parser.parseString(data);
    });
  });
  // when the parser ends the parsing we are ready to send the new data to the frontend
  parser.addListener('end', function(result) {

    // adding the time of the last update
    result.time = new Date();
    socket.volatile.emit('notification', result);
  });
});

和我的html代码:

    <script src="/node/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
   <script>
    // creating a new websocket
      var socket = io.connect('http://betty.dev');
      // on every message recived we print the new datas inside the #container div
      socket.on('notification', function (data) {
        $('.test').html(data.test.sample[0]);
        $('time').html('Last Update:' + data.time);
      });
    </script>

还有我的 xml 文件:

   <?xml version="1.0" encoding="ISO-8859-1"?>
<test>
    <sample>Hi 1!</sample>
</test>

尝试加载页面时出现此错误:

info  - socket.io started
   debug - client authorized
   info  - handshake authorized HPdjzW_pVy49g0bD6azs
   debug - setting request GET /socket.io/1/websocket/HPdjzW_pVy49g0bD6azs
   debug - set heartbeat interval for client HPdjzW_pVy49g0bD6azs
   debug - client authorized for 
   debug - websocket writing 1::
/Users/user/webserver/betty/www/node

fs.js:1051
    throw errnoException(process._errno, 'watch');

不知道是什么问题:/

4

2 回答 2

3

这是我的问题的解决方法:

var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    fs = require('fs'),
    homeFile = __dirname + '/home',
    jsonFile = __dirname + '/home/data';
app.listen(8080, 'test.dev');

function handler(req, res) {
    fs.readFile(homeFile, function(err, data) {
        if (err) {
            res.writeHead(500);
            return res.end('Error loading home');
        }

        res.writeHead(200);
        res.end(data);
    });
}

io.sockets.on('connection', function(socket) {
    fs.watchFile(jsonFile, function (curr, prev) {
            console.log('the current mtime is: ' + curr.mtime);
            console.log('the previous mtime was: ' + prev.mtime);


        fs.readFile(jsonFile, function(err, data) {
            if (err) throw err;

            var data = JSON.parse(data);
            socket.emit('notification', data);
        });
    });
});
于 2013-08-01T20:47:53.647 回答
1

在浏览器中试试这个 URL:

<script src="/socket.io/socket.io.js"></script>

(不,此 URL 与磁盘上的文件不对应,但没关系。socket.io 将正确地为浏览器 JS 提供服务)。

也没有必要也不建议让您的node_modules文件夹对浏览器可用。

更新

好的,所以在您修复了<script>标签中的 URL 之后,您现在拥有一个成功的 socket.io websocket 连接。耶!现在,您的fs代码正在引发异常。一方面,如果要readFile返回字符串而不是缓冲区,则需要传递编码:

fs.readFile(__dirname + '/example.xml', 'utf8', function(err, data) {

更新 2

fs.watch抛出异常也是如此。可能您的路径example.xml与真实文件系统不正确匹配。您正在记录__dirname。事情是否正确匹配?你确定吗?

于 2013-07-31T17:53:35.000 回答