torxx666
torxx666

Reputation: 31

Node.js stay connected without close after write

I try to explain my problem :

Client HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>demo game</title>
    <script src="/js/jquery-1.8.3.min.js"></script>
    <script src="http://127.0.0.1:8080"></script>
  </head>
  <body>
    <div id="messages"></div>
  </body>
</body>
</html>

Node server.js:

    var server = require('http').createServer(function(req, res){

               setInterval(function() {
                    mycode = html.readFileSync(__dirname+'/user1.txt');

                    socket.end(mycode+"\n");
                    console.log('data sent'+mycode);

                }, 1000);

         });

    server.listen(8080);

And let say the content of user1.txt is: $("#messages").append("<br> hello");

Sending the contain of user1.txt, is ok and execute on the client side, but, if the user1.txt contain change, I see it on the console.log('data sent'+mycode); , I suppose it what sent but not execute, why ??

I try with write() instead of end(), and I try with :

var net = require('net');
var html = require('fs');
var server = net.createServer({ allowHalfOpen: true},....

same!

once again : how can I sent 'live javascript code', on client side ?

thank in advance

Dany

Upvotes: 1

Views: 316

Answers (1)

Vadim Baryshev
Vadim Baryshev

Reputation: 26189

Browser can load your script before rendering of <div id="messages"></div>. In this case your code executes, bit without any effect. Try to change your code in user1.txt like this:

$(function() {
    $("#messages").append("<br> hello");
});

Upvotes: 1

Related Questions