angular - 无法通过本地 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');
}});
}
}
构建时出现以下错误 - 错误
解决方案
不支持在浏览器中使用 Azure IoT 节点 SDK。
推荐阅读
- angular - 在表单数组中添加和删除输入字段(反应式表单)
- c# - Hangfire - 多租户,ASP.NET Core - 解析正确的租户
- r - Databricks 上的 mgcv:k-index 的值与 RStudio 中的值不同
- java - 如何使用 jpa 规范向子查询添加和构建动态谓词?
- image - 如何从opencv中的轮廓形状中丢弃多余的线
- javascript - 在图片上单击位置时如何自动创建一个InputField?
- getter-setter - 调用 getter setter 时的基本工作
- javascript - 如何使用 javascript 正确更改 div 的大小?
- java - 如何为多个会话实施 cron 作业,每个会话都有自己的时间间隔
- javascript - 如何将输入字段放入 2 列?