目标:使用节点模块azure-iot-hub从浏览器(angular2)创建一个 azure iot hub 设备。
问题: azure-iot-common依赖于一个包crypto,它在浏览器中不起作用。
重建步骤:
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 - 建议将crypto切换到webcrypto
潜在解决方案:将加密切换到webcrypto - 需要重写azure-iot-common/lib/authorization.js
问题:
- 有没有人使用节点模块azure-iot-hub从浏览器创建集线器设备?
- 有没有人使用其他方法从浏览器创建集线器设备?
- 如果不回答 Q1,2 - 我的潜在解决方案是否可行?