【问题标题】:Device Orientation API not working with local webserver设备方向 API 不适用于本地网络服务器
【发布时间】:2020-04-24 21:46:05
【问题描述】:

在使用Device Orientation API 时,我发现了一些奇怪的东西。

以下在线演示完美运行(“compassneedscalibration”除外):https://www.audero.it/demo/device-orientation-api-demo.html

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

网页声明:

不支持设备方向事件
不支持 devicemotion 事件
不支持 compassneedcalibration 事件

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

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

*)python3 -m http.server

【问题讨论】:

    标签: javascript android html google-chrome orientation


    【解决方案1】:

    我发现您需要通过加密的 HTTPS 连接提供 wep 页面才能访问 Device Orientation 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()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 1970-01-01
      • 2012-03-16
      相关资源
      最近更新 更多