Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js-GET[[hostname]]/socket.io/1/-404(未找到)_Node.js_Socket.io - Fatal编程技术网

node.js-GET[[hostname]]/socket.io/1/-404(未找到)

node.js-GET[[hostname]]/socket.io/1/-404(未找到),node.js,socket.io,Node.js,Socket.io,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 t

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>

不确定问题出在哪里:/

在浏览器中尝试此URL:

<script src="/socket.io/socket.io.js"></script>
更新2


因此,
fs.watch
正在引发异常。您的
example.xml
路径可能与实际文件系统不匹配。您正在记录
\uuu dirname
。事情配得好吗?您确定吗?

在浏览器中尝试此URL:

<script src="/socket.io/socket.io.js"></script>
更新2


因此,
fs.watch
正在引发异常。您的
example.xml
路径可能与实际文件系统不匹配。您正在记录
\uuu dirname
。事情配得好吗?你确定吗?

以下是解决我问题的方法:

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);
        });
    });
});

以下是解决我问题的方法:

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);
        });
    });
});

是的,socket.io的东西现在正在工作,但是您的fs代码中有一个错误。@PeterLyons您知道当服务器位于另一个端口上时,您会怎么做吗?即:后端是localhost:9003,客户端是localhost:9002?端口不重要。只需准确地使用上面的URL。它总是正确的。您永远不想指定协议、主机名或端口。是的,socket.io的东西现在正在工作,但是fs代码中有一个错误。@PeterLyons您知道当服务器位于另一个端口时,您会如何做,例如:后端是localhost:9003,客户端是localhost:9002?端口不重要。只需准确地使用上面的URL。它总是正确的。您永远不想指定协议、主机名或端口。
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);
        });
    });
});