将文件发送到客户端的响应功能

时间:2020-02-07 08:24:04

标签: javascript node.js

我想在io.on事件列表器之后将html文件发送到客户端。 我首先使用普通

    `app.get("/", function(req, res){
    res.sendFile(__dirname + "/index.html")

})`

发送文件。但是然后我想在socket.on之后向客户端发送一个新文件:

    io.on("connection", function(socket){
    socket.on("password" function(){
 res.sendFile(__dirname + "loggedin.html");
})
 })

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以从服务器发出事件以通知客户端加载页面:

// In the server
io.on("connection", function (socket) {
  socket.on("password", function () {
    // ...Any other logic that needs to be here...
    // Emit an event to inform the client load the page
    socket.emit('load_index_page');
  })
})

..

// Somewhere in the client
socket.on('load_index_page', function () {
  window.location.assign("/index.html")
})
相关问题