Ionic app无法连接到Socket.IO

时间:2015-10-03 15:02:11

标签: javascript node.js socket.io ionic

我为Socket.IO连接设置了一个小节点应用程序:

$ionicPlatform.ready(function(device) {
    var socket = io('http://192.168.1.9:3000');

    if (window.cordova && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
        StatusBar.styleDefault();
    }
    alert('here');
    $state.go('signin');
});

在我的Ionic应用程序中,我有以下内容:

platform.ready

当Ionic应用程序启动时,我会显示警告,因此我知道Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883896073-0 Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883899867-1 Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883901481-2 Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883904606-3 事件正在触发。但Ionic应用程序从不连接到服务器上的套接字会话。

如果我使用Chrome检查Ionic应用程序,则会记录以下错误:

<meta>

但IP地址和端口号是正确的。

为什么Ionic不能连接到Socket.iO?

编辑 - 04/10/2015

我的Ionic项目正在使用cordova-plugin-whitelist插件,我在index.html中添加了内容安全政策{{1}}标记。

编辑05/10/2015

我也尝试删除并重新添加Android平台,但它仍无效。

1 个答案:

答案 0 :(得分:0)

可能存在 CORS 错误,

请使用此配置

let io = require('socket.io')(server, {
    origins: ["*"],
    cors: {
        origin: "*",
        methods: ["GET", "POST"]
    }
})

这将解决您的问题。