简体   繁体   中英

Server-side push using NodeJS

I want to implement a server-side push functionality using NodeJS. For now, all I want is a server that listens to requests to a specific URL (eg http://localhost:8000/inputData?data=hello )

Once this request is made, I want all clients viewing client.html to run alert(hello); . What I did is the following, on the server side:

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



var qs = require('querystring');

app.listen(8000);
console.log('server listening on localhost:8000');

function handler(req, res) {
  var url_parts = url.parse(req.url, true);
  var pathname = url_parts.pathname;
  switch(pathname){
      case '/inputData':
        var data = url_parts.query.data;
        socket.emit('notifyData', data); //socket is undefined
        break;
      default:
        fs.readFile(__dirname + '/client.html', function(err, data) {
          if (err) {
            console.log(err);
            res.writeHead(500);
            return res.end('Error loading client.html');
          }
          res.writeHead(200);
          res.end(data);
        });
  }

  ;
}

At the client side, in client.html :

<script>
    var socket = io.connect('http://localhost:8000');

    socket.on('notifyData', function (data) {
        alert(data);
    });
</script>

I get an error saying that socket is not defined. I know that I should define it earlier but I am not sure how to do that..

Is this the right way to approach such problem? or should the emit be in a different file eg inputData.html ? I'd rather complement the code I already have, because I might need to make a set of operations right before var data= url_parts.query.data

You have a design issue on your server as the socket variable is not defined at all. If what you're trying to do is to broadcast to all clients when any one hits that particular URL, then you can do that with:

 io.sockets.emit('notifyData', data);

If you're trying to emit to a single client socket, then you will have to find a way to figure out which socket in the io.sockets namespace it is that you're trying to send to.

From the docs , it looks like you haven't initialized the server correctly.

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(8000);

/*...*/

io.on('connection', function (socket) {
  socket.emit('notifyData', { hello: 'world' });
});

and on the client you should have

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost:8000');
  socket.on('notifyData', function (data) {
    alert(data);
  });
</script>

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