是否可以从浏览器创建 IoT 中心设备



目标:使用节点模块Azure-iot-hub从浏览器(Angular2(创建Azure IoT Hub设备。

问题: azure-iot-common依赖于一个包,加密,在浏览器中不起作用。

重新创建的步骤:

import { Component, OnInit } from '@angular/core';
let iothub = require(‘azure-iothub’);
const connectionString = ‘HostName=<my-host>.azure-devices.net;SharedAccessKeyName=<my-key-name>;SharedAccessKey=<my-key>=’;
@Component({
  selector: 'acn-shop',
  template: `
<div class="al-main">
  <div class="al-content container-fluid">
    <h1>Azure IoT Hub Devices</h1>
  </div>
</div>`
})
export class ShopComponent implements OnInit {
  constructor() {
  }
  public ngOnInit() {
    this.connect();
  }
  public connect() {
    console.log('Calling connect()');
    const registry = iothub.Registry.fromConnectionString(connectionString);
  }
}

从 Chrome 工具控制台

Error: Uncaught (in promise): TypeError: crypto.createHmac is not a function
TypeError: crypto.createHmac is not a function
    at Object.hmacHash (authorization.js:36)
    at Function.create (shared_access_signature.js:67)
    at Object.create (shared_access_signature.js:15)
    at Function.fromConnectionString (registry.js:65)
    at ShopComponent.Array.concat.ShopComponent.connect (shop.component.ts:32)
   … (goes on for a bit) ...
  • github上有一个类似的问题 - https://github.com/ipfs/js-ipfs/issues/270 - 建议将加密切换到webcrypto

潜在的解决方案:将加密货币切换到网络加密货币 - 需要重写azure-iot-common/lib/authorization.js

问题:

  1. 是否有人使用节点模块 azure-iot-hub 从浏览器创建中心设备?
  2. 有没有人使用其他方法从浏览器创建集线器设备?
  3. 如果对问题1,2是否定 - 我的潜在解决方案是否可行?

azure-iothub 节点模块是服务客户端 SDK,用于创建将用于管理 IoT 中心实例的后端应用程序,而不是用于设备。

在设备方面,需要使用设备客户端 SDK 模块 azure-iot-device。也就是说,即使解决了各种依赖项问题(例如发现的加密问题(,这仍然不起作用,因为 IoT 中心服务不支持 CORS,这意味着它不会接受来自 Web 客户端的请求。IoT 中心的 CORS 支持在我们的积压工作中,但尚未确定优先级,因此我们没有 ETA。

您可以尝试解决此限制的方法是在网站的后端运行设备客户端节点模块,当新的 Web 浏览器客户端连接到您的站点时,创建设备客户端的新实例。

库 https://github.com/PeculiarVentures/webcrypto-liner 会给你一个可以在浏览器(甚至是下层/IE(中使用的加密对象,https://github.com/PeculiarVentures/node-webcrypto-ossl 会给你一个用于Node的对象。

切换到网络加密应该没有问题,有关如何进行调用的示例,请参阅 https://github.com/diafygi/webcrypto-examples#hmac。

最新更新