设备方向API不适用于本地Web服务器

问题描述 投票:0回答:1

[使用Device Orientation API时,我发现有些奇怪。

以下在线演示非常有效(“ compassneedscalibration”除外:https://www.audero.it/demo/device-orientation-api-demo.html

但是当我在本地克隆Soucecode并通过本地Web服务器提供Web page *时,API似乎不再可用。虽然使用相同的浏览器选项卡。此外,JavaScript控制台中也不会出现任何消​​息,警告或错误。

网页状态:

deviceorientation事件不受支持devicemotion事件不受支持不支持指南针校准事件Screenshot of non working Webpage

我做错什么了吗?还是这是预期的行为或错误?我需要通过本地Web服务器提供Web应用程序。

我正在“ Android 7.1.1; VNS-L21 Build / NMF26V”上使用“ Chrome 79.0.3945.93”

*) python3 -m http.server

javascript android html google-chrome orientation
1个回答
0
投票

我发现您需要通过加密的HTTPS连接提供wep页面才能访问设备方向API以及某些mediaDevices。

在开发(而非生产)过程中提供HTTPS页面的简单方法是此简单的python网络服务器:

#!/usr/bin/env python3

# Based on http://www.piware.de/2011/01/creating-an-https-server-in-python/

# generate server.xml with the following command:
#    openssl req -new -x509 -keyout key.pem -out server.pem -days 365 -nodes
# run as follows:
#    python3 simple-https-server.py
# then in your browser, visit:
#    https://localhost:4443


import http.server
import ssl
import os

directory_of_script = os.path.dirname(os.path.abspath(__file__))

#server_address = ('localhost', 4443)
server_address = ('', 4443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
                               server_side=True,
                               certfile=os.path.join(directory_of_script, "server.pem") ,
                               keyfile=os.path.join(directory_of_script, "key.pem"),
                               ssl_version=ssl.PROTOCOL_TLS)
httpd.serve_forever()

© www.soinside.com 2019 - 2024. All rights reserved.