本文介绍了传感器在网络工作者阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,我们无法在网络工作人员中获取传感器数据。我不知道背后的原因。用例是我正在考虑在工作线程中获取地理位置数据,并只将处理后的版本发送到主线程。



对于GPS,这个表示它在工作线程中不受支持(没有理由给出)。我再次检查它,navigator.geolocation不支持在网络工作者。对于加速器和陀螺仪,我们有DeviceOrientationEvent和DeviceMotionEvent。但是我们需要通过窗口对象来使用它们,这对于工作线程是不可用的。



  • 所以我的问题是:


    1. 为什么网络工作者不支持navigator.geolocation?我没有看到任何理由在工作线程中阻止它。我认为应该没有线程安全或安全问题。

    2. navigator.geolocation是否属于导航器?这看起来像一个愚蠢的问题。但我无法很快在网上找到一个好的解释...... Web工作人员可以访问导航器对象。我很困惑为什么不支持navigation.geolocation。

    3. 为什么我们没有来自加速器和陀螺仪的原始传感器读数?我明白,抽象的事件是有用的。但有些情况下我们想要使用原始数据进行处理。我发现PhoneGap提供了访问原始传感器数据的方式,例如通过。但我的理解是,这样的API不属于标准化的HTML规范。
    4. 什么是相关的设计决定以决定是否应该支持工作者线程与否? HTML中的常规传感器读取支持目前根据被搁置。看到当前的传感器支持(GPS,加速器,陀螺仪),我想我们会得到抽象的DOM事件。并且它很可能通过导航器对象具有原始传感器数据读数。


    解决方案

    好的。在阅读了一些Chromium代码之后,我现在回答了我自己的问题2。我仍然没有回答其他3个问题...



    问题2的答案:navigator.geolocation是否属于导航器?



    navigator.geolocation仅在主线程中属于导航器,但不属于工作线程中的导航器。



    主要原因是即使工作线程中的导航器看起来与主线程中的导航器完全相同,但这两个导航器在C ++端有独立的实现。这就是为什么worker线程不支持navigator.geolocation的原因。



    相关代码位于和。您可以看到它们是.idl文件中的两个独立接口。而且它们在绑定的C ++端有独立的实现。导航器是,而WorkerNavigator是属于。 然而,在JavaScript方面,它们有相同的名称:navigator。那么,我明白这两位导航员有两个不同的范围,所以没有名称冲突。但是,当我在JavaScript中使用这些API时,如果它们具有相同的名称,我希望主线程和辅助线程具有类似的行为。这就是模糊不清的情况。


    It seems that we can not get sensor data in the web workers. I wonder the reason behind it. The use case is that I am thinking about getting geolocation data in the worker thread and only send the processed version to the main thread.

    For GPS, this post says it is not supported in the worker thread (no reason is given). And I double checked it, navigator.geolocation is not supported in web workers. For accelerator and gyroscope, we have DeviceOrientationEvent and DeviceMotionEvent. But we need to use them through the window object, which is not available to the worker thread. The same situation applies to ambient light event.

    So my questions are:

    1. Why navigator.geolocation is not supported in web workers? I don't see any reason to prevent it in the worker thread. I think there should be no thread safety or security problems.
    2. Does navigator.geolocation belong to navigator? This looks like a silly question. But I cannot find a good explanation online quickly... Web workers have access to the navigator object. And I am confused why navigation.geolocation is not supported.
    3. Why don't we have raw sensor readings from accelerator and gyroscope? I understand that the abstracted event is useful. But there are cases we want to use the raw data for processing. I find that PhoneGap provides ways to access raw sensor data, e.g., through navigator.accelerometer. But my understanding is that such API does not belong to the standardized HTML specification.
    4. What are the related design decisions to decide whether general sensor reading should be supported in the worker thread or not? General sensor reading support in HTML is currently shelved according to W3C Device APIs Working Group. Seeing current sensor support (gps, accelerator, gyro), I think we will get abstracted DOM events. And it is likely to have raw sensor data readings through the navigator object.
    解决方案

    OK. After reading some Chromium code, I have the answer to my own question 2 now. I still have no answer to the other 3 questions...

    Answer to question 2: Does navigator.geolocation belong to navigator?

    navigator.geolocation belongs to navigator in the main thread only, but doesn't belong to navigator in the worker thread.

    The main reason is that even though the navigator in worker thread looks exactly the same as the one in main thread, those two navigators have independent implementations on the C++ side. That is why navigator.geolocation is not supported in the worker thread.

    The related code is in Navigator.idl and WorkerNavigator.idl in Chromium code. You can see that they are two independent interfaces in the .idl files. And they have independent implementations on the C++ side of the binding. Navigator is an attribute of DOMWindow, while WorkerNavigator is an attribute of WorkerGlobalScope.

    However, on the JavaScript side, they have the same name: navigator. Well, I understand that the two navigators are in two different scopes, so there is no name conflict. But when I use the APIs in JavaScript, I expect similar behavior on both main and worker threads if they have the same name. That's how the ambiguity happens.

    这篇关于传感器在网络工作者阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-01 18:09