NodeJS Semaphore 无限加载页面

发布于 2025-01-17 05:16:10 字数 777 浏览 4 评论 0原文

我正在编写一个程序,当客户端加入“/json”时,它会为您提供一个 json。我想用一个信号量来调整这个,每次有 1 个客户端请求资源,如果它发生了变化,那么它就会给你。我在 npm 中使用一个名为“semaphore”的包(https://www.npmjs.com/package /信号量)。

kasRouter.get(path + '/json',(req,res)=>{
    sem.take(async () => {
        let toggle2 = await utils.getToggleKas()
        console.log(req)
        if(toggle2 != toggle){
            sem.leave()
            toggle = toggle2
            console.log(toggle)
            res.json({
                'kas':toggle
            })
        }
    })
})

如您所见,当客户端加入路径时,我使用 sem.take() 函数让客户端等待离开它。切换和切换2基本上是2个变量,它们在json(在数据库中)中采用布尔值。当主路由启动时,第一个切换获取布尔值,当客户端加入 json 路径时,等待切换更改。此代码中的问题是客户端无休止地尝试在页面中进行连接,但服务器却没有响应。

i'm writing a program that when a client joins to "/json", it gives you a json. I want to adjust this with a semaphore where 1 client per time requests the resource and if it's changed, then it gives to you. I'm using a package in npm called "semaphore" (https://www.npmjs.com/package/semaphore).

kasRouter.get(path + '/json',(req,res)=>{
    sem.take(async () => {
        let toggle2 = await utils.getToggleKas()
        console.log(req)
        if(toggle2 != toggle){
            sem.leave()
            toggle = toggle2
            console.log(toggle)
            res.json({
                'kas':toggle
            })
        }
    })
})

as you can see, when the client joins in the path, i use sem.take() function to let the client wait for leaving it. toggle and toggle2 are basically 2 vars that take a boolean in the json (in a database). When the main route starts the first toggle gets the boolean and when the client joins in the json path waits for toggle chages. The problem in this code is that client tries infinit endlessly to connect in the page but without a response from the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2025-01-24 05:16:10

好吧,你似乎错过了一些东西。也就是说,您不会调用 res.end,并且仅当 await utils.getToggleKas() 的结果发生更改时才会调用 sem.leave

请参阅如果下面的代码片段有帮助

kasRouter.get(path + '/json',(req,res)=>{
    sem.take(async () => {
        let toggle2 = await utils.getToggleKas()
        console.log(req)
        if(toggle2 != toggle){
            toggle = toggle2
            console.log(toggle)
            res.json({
                'kas':toggle
            })
        }
        sem.leave() //after every requests, leaves it for a new one
    })
    //res.end() //ends the request in case this doesn't happen elsewhere
    //see if commenting this out ends the problem?
})

Ok, you seem to be missing some things. Namely, you don't call res.end and sem.leave is only called if the result from await utils.getToggleKas() changes

See if the code snippet below helps

kasRouter.get(path + '/json',(req,res)=>{
    sem.take(async () => {
        let toggle2 = await utils.getToggleKas()
        console.log(req)
        if(toggle2 != toggle){
            toggle = toggle2
            console.log(toggle)
            res.json({
                'kas':toggle
            })
        }
        sem.leave() //after every requests, leaves it for a new one
    })
    //res.end() //ends the request in case this doesn't happen elsewhere
    //see if commenting this out ends the problem?
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文