gpt4 book ai didi

jquery - Socket.io 不改变 HTML

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:38 24 4
gpt4 key购买 nike

在我的 Node 应用程序中,我使用 Socket.io 将文本发送到客户端。我可以看到文本已成功从服务器发送到客户端,但在我的客户端 jQuery 文件中,socket.io 没有更新我的 html。

服务器.js:

const app = express();
const server = app.listen(3000, () => {
console.log(`Web server up on port ${port}`);
});

let io = require('socket.io').listen(server);
io.on('connection', function(socket) {
console.log('Connected socket!');
});

app.post('/findbrokenlinks', (req, res) => {

...

// here i'm iterating through an array of links and if they are broken, I'm sending them to the client side

links.forEach( (linkUrl) => {
if (isBroken(linkUrl)) {
io.sockets.emit("result", linkUrl);
}
}

}

index.js:

var socket = io('ws://localhost:3000', {transports: ['websocket']});

socket.on("result", function (data) {
console.log(data);
$('#results').append($('<p>').text(data));
});

索引.hbs:

<div id="results">

</div>

在我的 Node 控制台和 JavaScript 客户端控制台中,我都可以看到信息已交换。但我的网页在加载时卡住,并且不显示我更新的 results div。

这是我的客户端控制台(Safari Inspect Element)的样子:

[Log] https://gmail.com/deadlink (server.js, line 74)
[Log] https://www.facebook.com/awg;alwmg;lawkg (server.js, line 74)
[Log] https://github.com/alwfl (server.js, line 74)
> Selected Element
< <div id="results">
<p>https://gmail.com/deadlink</p>
<p>https://www.facebook.com/awg;alwmg;lawkg</p>
<p>https://github.com/alwfl</p>
</div>

最佳答案

我明白了。我的网页现在通过添加以下代码行实时更新:

const app = express();
const server = app.listen(3000, () => {
console.log(`Web server up on port ${port}`);
});

let io = require('socket.io').listen(server);
io.on('connection', function(socket) {
console.log('Connected socket!');
});

app.post('/findbrokenlinks', (req, res) => {

...

// here i'm iterating through an array of links and if they are broken, I'm sending them to the client side

for each (linkUrl in links) {
if (isBroken(linkUrl)) {
io.sockets.emit("result", linkUrl);
}
}

res.redirect('/findbrokenlinks'); // ADD THIS LINE !!

}


关于jquery - Socket.io 不改变 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57317842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com