简体   繁体   中英

Show random numbers on html sent from Node.js

I am trying to show random numbers generated in node.js to a file called display.html and show on port 3000. When I run the node code in server.js I get confirmation that data is being transmitted, but I see nothing on localhost:3000. I am using node.js express version 4.7.1 and socket.io version 1.0.6.

Thanks in advance!

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
 res.sendfile('display.html');
});

io.on('connection', function(socket){
 console.log('a user connected');
});

setInterval(function(){
console.log('emitting');
io.emit('coordinate', {
x: Math.random(),
y: Math.random(),
z: Math.random()
});
}, 3000);

http.listen(3000, function(){
 console.log('listening on *:3000');
});

Here is the html where I take in the data:

<html>
<body>
<ul>
</ul>

<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
 var socket = io('192.734.....:3000');

 socket.on('coordinate', function(obj){
     $('ul').append($('<li>').text(JSON.stringify(obj)));
 });

 oncoording
    io.emit('coord'.., obj)
</script>
</body>
</html>

Try to change your HTML for this:

  <html>
  <body>
    <ul>
    </ul>

    <script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io('127.0.0.1:3000');

      socket.on('coordinate', function(obj){
        $('ul').append($('<li>').text(JSON.stringify(obj)));
      });
    </script>
  </body>
  </html>

Tha last two lines of script is throwing a syntax error on browser:

oncoording
  io.emit('coord'.., obj)

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