Android 未找到NativeScript WebSocket模块:错误:Can';t解决';nativescript websockets';
我有一个Angular Web应用程序,我想使用NativeScript作为Android应用程序运行 为了让websocket在Android上工作,我使用 这适用于Android应用程序Android 未找到NativeScript WebSocket模块:错误:Can';t解决';nativescript websockets';,android,angular,websocket,nativescript,nativescript-angular,Android,Angular,Websocket,Nativescript,Nativescript Angular,我有一个Angular Web应用程序,我想使用NativeScript作为Android应用程序运行 为了让websocket在Android上工作,我使用 这适用于Android应用程序 require("nativescript-websockets"); this.websocket = new WebSocket("myUrl, "myProtocol"); 我在一个服务中添加了上述内容,该服务将创建一个websocket到服务器
require("nativescript-websockets");
this.websocket = new WebSocket("myUrl, "myProtocol");
我在一个服务中添加了上述内容,该服务将创建一个websocket到服务器
但是,当我尝试运行web时,会出现以下错误:
未找到模块:错误:无法解析“nativescript websockets”
当我删除require(“nativescriptwebsockets”)代码>从服务中,它适用于web
我需要做哪些更改才能使其同时适用于Web和移动设备?您不能在应用程序的Web部件上使用nativescript WebSocket
。由于nativescript websockets
包支持基于浏览器的界面,因此您的解决方案看起来非常简单
创建基于通用websockets接口的服务基类。这可以是一个真实的类,也可以是一个抽象的类,这取决于您的需要
//服务/price-provider.base.ts
导出类PriceProviderBase{
私有套接字:WebSocket;
createSocket():void{
this.socket=newwebsocket(“此处的url”);
this.socket.onopen=this.handleOpen.bind(this);
this.socket.onclose=this.handleClose.bind(this);
this.socket.onmessage=this.handleMessage.bind(this);
this.socket.onerror=this.handleError.bind(this);
}
handleOpen(){
//在这里做你的事。。。
}
...
}
将此基本实现扩展为将在web和nativescript环境中使用的真实服务
对于web:
//服务/price-provider.service.ts
从“@angular/core”导入{Injectable};
从“~/services/price provider.base”导入{PriceProviderBase};
@注射的({
providedIn:'根',
})
导出类PriceProviderService扩展PriceProviderBase{}
对于nativescript:
//服务/price-provider.service.tns.ts
从“@angular/core”导入{Injectable};
从“~/services/price provider.base”导入{PriceProviderBase};
需要('nativescript-websockets');
@注射的({
providedIn:'根',
})
导出类PriceProviderService扩展PriceProviderBase{}
如果web和nativescript应用程序上的逻辑相同,那么这两种实现中唯一的区别就是require('nativescript-websockets')代码>行
require("nativescript-websockets");
this.websocket = new WebSocket("myUrl, "myProtocol");