Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 未找到NativeScript WebSocket模块:错误:Can';t解决';nativescript websockets';_Android_Angular_Websocket_Nativescript_Nativescript Angular - Fatal编程技术网

Android 未找到NativeScript WebSocket模块:错误:Can';t解决';nativescript websockets';

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到服务器

我有一个Angular Web应用程序,我想使用NativeScript作为Android应用程序运行

为了让websocket在Android上工作,我使用

这适用于Android应用程序

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");