node.js - how to make a simple live page update?

前端 未结 2 1274
我寻月下人不归
我寻月下人不归 2020-12-23 12:34

I\'m very very new to node.js, but there\'s actually only one simple thing that I am trying to achieve by learning the language.

I\'d like to create a webpage, where

2条回答
  •  春和景丽
    2020-12-23 13:04

    Take a look at Socket.IO http://socket.io/#how-to-use

    when the server decides to broadcast a change use:

    io.sockets.emit('update-msg', { data: 'this is the data'});

    on the client first connect socket.io and then wait for the "update-msg" event and update your dom:

    var socket = io.connect('http://localhost');
    socket.on('update-msg', function (msg) {
        console.log(msg);
        $('#mydiv').html(msg.data)
    });
    

提交回复
热议问题