首页 > 解决方案 > 无法通过本地 ionic 应用程序连接到 azure iot hub

问题描述

我正在尝试将我的 ionic 4 应用程序(角度 7)与 azure iot hub 连接。我正在尝试将 msgs 从我的应用程序发送到 iot hub。在我的 ionic 项目中下载了“azure-iothub”,并尝试导入“客户端”以形成连接,但在构建时出现错误。

尝试将这些特定模块设置为 false 浏览器,它会生成警告但应用程序不会在 localhost 上加载。还尝试了 http post 请求,但也出现了 CORS 错误,因为“localhost is not allowed to access”。


import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import {Client} from 'azure-iothub';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
    var connectionString = '**********';

    var client = Client.fromConnectionString(connectionString);

    client.open(function (err) {
      if (err) {
        console.error('Could not connect: ' + err.message);
      } else {
        console.log('Service client connected');
    }});

  }
}

构建时出现以下错误 - 错误

标签: angularazureionic-frameworkazure-iot-hub

解决方案


不支持在浏览器中使用 Azure IoT 节点 SDK。


推荐阅读