简体   繁体   中英

node.js - GET [[hostname]]/socket.io/1/ - 404 (Not Found)

Socket.io doesn't seem to be serving up its socket.io.js file with connect.

here is my server.js code:

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

and my html code:

    <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>

And my xml file:

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

I'm getting this error when trying to load the page:

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

Not sure what is the problem :/

here is the fix to my problem:

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

Try this URL in the browser:

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

(No, this URL does not correspond to a file on disk, but that's OK. socket.io will server the browser JS correctly).

It's also not necessary or advisable to make your node_modules folder available to the browser.

Update

OK, so after you fixed the URL in your <script> tag, you now have a successful socket.io websocket connection. Yay! Now, your fs code is throwing an exception. For one thing, if you want readFile to return a string and not a buffer, you need to pass an encoding:

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

Update 2

So fs.watch is throwing an exception. Probably your path to example.xml doesn't correctly match the real filesystem. You are logging __dirname . Do things match up properly? Are you sure?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM