Chrome 设备方向API无法与本地Web服务器配合使用

pgx2nnw8  于 2023-05-11  发布在  Go
关注(0)|答案(2)|浏览(116)

在使用Device Orientation API时,我注意到一些奇怪的事情。
下面的在线演示工作得很完美(除了“compassneedcalibration”):https://www.audero.it/demo/device-orientation-api-demo.html
但是,当我在本地克隆Soucecode并通过本地Web服务器提供Web page * 时,API似乎不再可用。虽然使用相同的浏览器选项卡。JavaScript控制台中也不会出现任何消息、警告或错误。
该网页指出:
不支持deviceorientation事件
devicemotion事件不支持
不支持compassneedcalibration事件x1c 0d1x
我做错什么了吗?或者这是一个有意的行为还是一个bug?我将需要通过本地Web服务器提供我的Web应用程序。
我正在使用“Chrome 79.0.3945.93”在“Android 7.1.1; VNS-L21 Build/NMF26V”

  • python3 -m http.server
e4yzc0pl

e4yzc0pl1#

我发现你需要通过加密的HTTPS连接提供wep页面,以便访问设备定位API和一些媒体设备。
在开发(而非生产)期间提供HTTPS页面的一种简单方法是这个简单的python web服务器:

#!/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()
7eumitmz

7eumitmz2#

事实上,这节省了我大量的时间!
如果你使用像xampp这样的东西来本地托管,https应该已经设置好了,所以只需要输入https而不是http,一切就开始工作了。

相关问题