简体   繁体   English

使用 socket.io 将数据从 nodejs 传输到 html

[英]Transfer data from nodejs to html using socket.io

I am trying to transfert data from nodejs to html using socket.io.我正在尝试使用 socket.io 将数据从 nodejs 传输到 html。 First I tried to transfert text from nodejs to html and it works:首先,我尝试将文本从 nodejs 传输到 html 并且它有效:

nodejs code:节点代码:

app.post('/timer', function(req, res){                  
    res.sendFile(__dirname + '/public/status.html');
    io.emit('messageFromServer', "text");
});

html code: html 代码:

<ul id="messagesList">
                    
</ul>
            <script src="/socket.io/socket.io.js"></script>
            <script>
            var socket = io();
            var list = document.getElementById("messagesList");
            socket.on('messageFromServer', function (data) {
            var item = document.createElement('li');        
            item.innerHTML = data;
            list.appendChild(item);
            });
            </script>

Text appears between ul id="messagesList" AND /ul.文本出现在 ul id="messagesList" AND /ul 之间。

Now I want to transfer variable from nodejs to html.现在我想将变量从 nodejs 传输到 html。 The variable is emit from python and it works.该变量是从 python 发出的,它可以工作。 But I can't write this variable into the html page and I don't know why.但是我无法将这个变量写入 html 页面,我不知道为什么。

Nodejs code: info is the variable from python. Nodejs 代码:info 是来自 python 的变量。

app.post('/timer', function(req, res){                  
    res.sendFile(__dirname + '/public/status.html');
    var info = req.body;
    io.emit('messageFromServer', info);
    console.log(info)
});

When I launch the python code, the console.log(info) write: { time: '10' }当我启动 python 代码时,console.log(info) 写入:{ time: '10' }

html code (the same): html代码(同):

            <ul id="messagesList">
            
            </ul>
            <script src="/socket.io/socket.io.js"></script>
            <script>
                var socket = io();
                var list = document.getElementById("messagesList");
                
                socket.on('messageFromServer', function (data) {
                var item = document.createElement('li');        
                item.innerHTML = data;
                list.appendChild(item);

                });
            </script>

When I launch the python code, the html write: [objet, Object], but I want: " time 10 ".当我启动 python 代码时,html 写:[objet, Object],但我想要:“time 10”。 How can I adapt my code?如何调整我的代码? Thanks.谢谢。

=============================== ================================

Now it works with these codes: Nodejs:现在它可以使用以下代码:Nodejs:

app.post('/timer', function(req, res){                  
    res.sendFile(__dirname + '/public/status.html');
    var info = req.body;
    io.emit('messageFromServer', info);
    console.log(info)
});

and html:和 html:

            <ul id="messagesList">
            
            </ul>
            <script src="/socket.io/socket.io.js"></script>
            <script>
                var socket = io();
                var list = document.getElementById("messagesList");
                socket.on('messageFromServer', function (data) {
                var item = document.createElement('li'); 
                item.innerHTML = data.time;
                list.appendChild(item);
                });
            </script>

Thanks for helping me.谢谢你帮助我。

This maybe solve your problem.这也许可以解决你的问题。

item.innerHTML = data.time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM