node.js - GET [[hostname]]/socket.io/1/ - 404 (Not Found) -
socket.io doesn't seem serving socket.io.js file connect.
here 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 server ( localhost:8000 ) app.listen(8080); // on server started 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 new websocket keep content updated without ajax request io.sockets.on('connection', function(socket) { console.log(__dirname); // watching xml file fs.watch(__dirname + '/example.xml', function(curr, prev) { // on file change can read new xml fs.readfile(__dirname + '/example.xml', function(err, data) { if (err) throw err; // parsing new xml data , converting them json file parser.parsestring(data); }); }); // when parser ends parsing ready send new data frontend parser.addlistener('end', function(result) { // adding time of last update result.time = new date(); socket.volatile.emit('notification', result); }); });
and html code:
<script src="/node/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script> <script> // creating new websocket var socket = io.connect('http://betty.dev'); // on every message recived print new datas inside #container div socket.on('notification', function (data) { $('.test').html(data.test.sample[0]); $('time').html('last update:' + data.time); }); </script>
and xml file:
<?xml version="1.0" encoding="iso-8859-1"?> <test> <sample>hi 1!</sample> </test>
i'm getting error when trying load page:
info - socket.io started debug - client authorized info - handshake authorized hpdjzw_pvy49g0bd6azs debug - setting request /socket.io/1/websocket/hpdjzw_pvy49g0bd6azs debug - set heartbeat interval client hpdjzw_pvy49g0bd6azs debug - client authorized debug - websocket writing 1:: /users/user/webserver/betty/www/node fs.js:1051 throw errnoexception(process._errno, 'watch');
not sure problem :/
here fix 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); }); }); });
Comments
Post a Comment