[英]ionic2 WebSocket is not working in Android App
Trying to get WebSocket work in an ionic2 Android app. 试图使WebSocket在ionic2 Android应用程序中工作。 In browser mode it is working fine .
在浏览器模式下,它可以正常工作 。
I installed https://github.com/knowledgecode/WebSocket-for-Android and followed all the instructions: - installed cordova-plugin-whitelist - set a Content-Security-Policy (CSP) - set config.xml: 我安装了https://github.com/knowledgecode/WebSocket-for-Android,并按照所有说明进行了操作:-安装了cordova-plugin-whitelist-设置了内容安全策略(CSP)-设置了config.xml:
<access origin="*"/>
<allow-intent href="ws://*/*:*"/>
<allow-intent href="wss://*/*:*"/>
But there is no connection to my server when i test with ionic run android 但是当我用ionic run android测试时,没有连接到我的服务器
connect(){
this.ws = new WebSocket("ws://192.168.178.170:8000");
this.ws.onopen = () => {
console.log('open');
};
this.ws.onmessage = (event) => {
console.log('new message: ' + event.data);
this.messages.push(event.data);
};
this.ws.onerror = () => {
console.log('error occurred!');
};
this.ws.onclose = (event) => {
console.log('close code=' + event.code);
this.connect();
};
Did i missed something? 我错过了什么吗?
I've never used Ionic 2, but at least in Corova, a CSP meta tag is enabled in html pages by default. 我从未使用过Ionic 2,但至少在Corova中,默认情况下在html页面中启用了CSP元标记。 If the tag is set in your html page, you need to append a directive to it to allow WebSocket access.
如果在html页面中设置了标记,则需要向其附加指令以允许WebSocket访问。
For instance: 例如:
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src ws://192.168.178.170:8000">
Take notice of the end of the above. 请注意上述结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.