2016-08-01 54 views
0

我的Ionic正在与web浏览器中使用socket.io。但是,当我尝试编译我的应用程序到Android,似乎socket.io无法连接到我的nodejs服务器。cordova angular无法连接到socket.io

正如我安慰一切为连接事件我的节点服务器,它不会如下图所示触发connection事件:

io.on('connection',function(socket){ 
    console.log('My socket id is '+socket.id); 
}); 

我的节点主机正常工作时,我尝试在浏览器中运行应用程序。但使用从科尔多瓦提取的android应用程序似乎无法连接。

UPDATE:这是我​​3210文件在我的应用程序

<?xml version='1.0' encoding='utf-8'?> 
<widget id="com.myapp.pagemanager" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> 
    <name>PageManager</name> 
    <description> 
     A sample Apache Cordova application that responds to the deviceready event. 
    </description> 
    <author email="[email protected]" href="http://cordova.io"> 
     Apache Cordova Team 
    </author> 
    <content src="index.html" /> 
    <plugin name="cordova-plugin-whitelist" spec="1" /> 
    <access origin="*" /> 
    <allow-intent href="http://*/*" /> 
    <allow-intent href="https://*/*" /> 
    <allow-intent href="tel:*" /> 
    <allow-intent href="sms:*" /> 
    <allow-intent href="mailto:*" /> 
    <allow-intent href="geo:*" /> 
    <platform name="android"> 
     <allow-intent href="market:*" /> 
    </platform> 
    <platform name="ios"> 
     <allow-intent href="itms:*" /> 
     <allow-intent href="itms-apps:*" /> 
    </platform> 
</widget> 
+0

您使用的是离子吗? –

+0

你使用'socket.io-client'作为你的依赖吗?您是否将服务器域列入了白名单(例如,查看[白名单]的cordova文档(https://cordova.apache.org/docs/en/latest/guide/appdev/whitelist/index.html))? – Beat

+0

@DevidFarinelli是的 –

回答

0

我已经解决了我的问题。我所做的就是让它在模拟器中工作,以便我可以调试或轻松查看错误。我发现手机的路径总是在file://等于当我使用本地脚本代码获取当前路径时启动。这就是为什么它没有连接到我的Nodejs服务器。现在一切都是固定的。谢谢! SO'ers。