Ionic app won't connect to Socket.IO

梦想的初衷 提交于 2020-01-13 14:29:37

问题


I have a small node app set up for Socket.IO connections:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

io.on('connection', function (socket) {
    console.log('socket connected');

    socket.on('disconnect', function () {
        console.log('socket disconnected');
    });

    socket.emit('text', 'wow. such event. very real time.');
});

server.listen(3000, function() {
    console.log('Socket.io Running');
});

In my Ionic app I have the following:

$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');
});

When the Ionic app starts up, I get the alert displayed so I know the platform.ready event is firing. But the Ionic app never connects to the socket session on the server.

If I inspect the Ionic app using Chrome, I get the following errors logged:

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

But the IP address and port numbers are correct.

Why won't Ionic connect to to Socket.iO?

EDIT - 04/10/2015

My Ionic project is using the cordova-plugin-whitelist plugin and I have added a Content Security Policy <meta> tag in my index.html.

EDIT 05/10/2015

I have also tried removing and re-adding the Android platform but it still does not work.

来源:https://stackoverflow.com/questions/32923998/ionic-app-wont-connect-to-socket-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!