在 Windows 10 个桌面切换到锁定屏幕时阻止 Node 应用程序停止

Prevent stopping of Node app while Windows 10 desktop switches to lock screen

我正在使用与 websocket 服务器通信的 Electron 实现 Node 应用程序。只要用户登录,该应用程序就可以顺利运行。如果用户一段时间不活动,则会显示 Windows 10 系统的锁定屏幕。只要用户解锁桌面,websocket 连接似乎就会停止。

是否有机会打开 websocket 连接并且 运行 如果客户端处于锁定屏幕状态?

也许有一个不让系统通过节点锁定屏幕的解决方案?

自己找到解决方案:

electron 提供 powerSaveBlocker class

示例:

const { powerSaveBlocker } = require('electron')
const id = powerSaveBlocker.start('prevent-display-sleep')
console.log(powerSaveBlocker.isStarted(id))
powerSaveBlocker.stop(id)

可以在官方 api 文档中找到更多详细信息:

https://electronjs.org/docs/api/power-save-blocker