简体   繁体   English

Heroku 环境中的唤醒锁

[英]Wake lock in Heroku Environment

I have a website built with Python Flask (hosted on Heroku) and I am utilizing the js Wake Lock API to keep the user's device alive.我有一个使用 Python Flask (托管在 Heroku 上)构建的网站,我正在使用 js Wake Lock API 来保持用户的设备存活。 Basically my site connects with the NHL's API and plays a goal horn when your team scores.基本上,我的网站与 NHL 的 API 相连,并在您的球队得分时播放球门号角。 I have added a Wake Lock toggle to the page where the app runs to allow users to enable the feature.我在应用程序运行的页面上添加了唤醒锁定开关,以允许用户启用该功能。

Here is my problem: I am being told Wake Lock is not compatible with the browser when I deployed to my production environment.这是我的问题:当我部署到生产环境时,我被告知 Wake Lock 与浏览器不兼容。 Everything worked just fine locally when running in the same browser.在同一个浏览器中运行时,一切都在本地运行良好。 Here is my Wake Lock code:这是我的唤醒锁代码:

const checkbox = document.querySelector('#wakeLock');
const statusElem = document.querySelector('#wakeLockStatus')

const updateUI = (status = 'acquired') => {
  const acquired = status === 'acquired' ? true : false;
  checkbox.dataset.status = acquired ? 'on' : 'off';
  checkbox.checked = acquired ? true : false;
  statusElem.textContent = `Wake Lock ${acquired ? 'is active!' : 'is off.'}`;
}

// test support
let isSupported = false;

if ('wakeLock' in navigator) {
    isSupported = true;
    statusElem.textContent = 'Wake lock API is supported in your browser!';
} else {
    checkbox.disabled = true;
    statusElem.textContent = 'Wake lock is not supported by this browser.';
}

if (isSupported ) {
    // create a reference for the wake lock
    let wakeLock = null;

    // create an async function to request a wake lock
    const requestWakeLock = async () => {
        try {
            wakeLock = await navigator.wakeLock.request('screen');

            // change up our interface to reflect wake lock active
            updateUI()

            // listen for our release event
            wakeLock.onrelease = function(ev) {
                console.log(ev);
            }
            wakeLock.addEventListener('release', () => {
            // if wake lock is released alter the button accordingly
                updateUI('released');
            });

            console.log(wakeLock)

        } catch (err) {
            // if wake lock request fails - usually system related, such as battery
            checkbox.checked = false;
            statusElem.textContent = `${err.name}, ${err.message}`;
        }
    } // requestWakeLock()

    // toggle
    checkbox.addEventListener('click', () => {
        // if wakelock is off request it
        if (checkbox.dataset.status === 'off') {
          requestWakeLock()
        } else { // if it's on release it
          wakeLock.release()
            .then(() => {
              wakeLock = null;
            })
        }
    })

}

Like I said, this was working perfectly when testing locally, but when I deployed it no longer works.就像我说的,这在本地测试时工作得很好,但是当我部署它时它就不再工作了。 It is this part that is failing because I see the "Wake Lock is not supported in this browser" message on the front end.正是这部分失败了,因为我在前端看到“此浏览器不支持唤醒锁”消息。 The app is hosts on Heroku, if that matters.该应用程序是 Heroku 上的主机,如果这很重要的话。

// test support
let isSupported = false;

if ('wakeLock' in navigator) {
    isSupported = true;
    statusElem.textContent = 'Wake lock API is supported in your browser!';
} else {
    checkbox.disabled = true;
    statusElem.textContent = 'Wake lock is not supported by this browser.';
}

Link to website: www.nhlcompanion.com Pick a team and click start to see the Wake Lock toggle and message I am talking about.链接到网站: www.nhlcompanion.com选择一个团队并单击开始以查看我正在谈论的唤醒锁定切换和消息。

Thank you in advance for any help!!预先感谢您的任何帮助!!

I found the solution.我找到了解决方案。 Wake Lock requires HTTPS and I was just using an HTTP connection as I was still using the free Heroku plan.唤醒锁需要 HTTPS而我只是使用 HTTP 连接,因为我仍在使用免费的 Heroku 计划。 I have upgraded and it is working as expected.我已经升级,它按预期工作。

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

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