1

I'm in the process of making an ionic app for android that will communicate with a simple Node.js server over a socket. For this purpose I'm using socket.io. I'm also using Angular.js on the client side.

I've been struggling to get the connection to work for about one week. I wan't to add here that I am in no way an expert, or even used to, javascript. I have been weeding out problems and bugs but this one error has me broken.

Failed to load resource: the server responded with a status of 404 (Not Found) http://EXTERNAL_IP:3000/socket.io/?EIO=3&transport=polling&t=1431009277925-0

To me this seems like the client is trying to get the socket.io files from the server, but the file doesn't exist on the server. I have the socket.io.js file locally in the client's file structure and links it in html as so:

<script src="js/vendor/socket-io.js"></script>

I get this error in the console both when i run via the live reload server and when the app is deployed to my Galaxy S5 running android 5.0

I have tried a lot of the suggestions both from this site and from others but to no avail. Maybe there is something with my hardware setup that doesn't want to play nice?

I have been following this tutorial: http://www.htmlxprs.com/post/6/creating-a-realtime-image-sharing-app-with-ionic-and-socketio-tutorial

But that code refuses to work as well.

server.js

var server = require('http').createServer();
var io = require('socket.io')(server);

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

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

  socket.on('echo', function(msg){
    console.log("Recieved msg: " + msg);
    socket.emit('echo', msg);
  })

});

server.listen(3000);

services.js

angular.module('MyApp.services', []).factory('socketio', function($rootScope) {
  var socket = io.connect("http://EXTERNAL_IP:3000");
  return {
    on: function (eventName, callback) {
      socket.on(eventName, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      });
    },
    emit: function (eventName, data, callback) {
      socket.emit(eventName, data, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          if (callback) {
            callback.apply(socket, args);
          }
        });
      })
    }
  };
});

controllers.js

angular.module("MyApp.controllers", [])
.controller("SocketCtrl", ["$scope", 'socketio', function($scope, socketio){
  console.log("attempting to connect");
  socketio.on("connect", function(){
    console.log("Connected");
  });
  socketio.on("echo", function(msg){
    console.log("Received msg: " + msg);
  });
}]);

If anyone has any pointers at all I would be really thankful.

  • had same problem bro .. check this out [you need to use cordova whitelist plugin][1] [1]: http://stackoverflow.com/questions/30132885/ionic-app-cannot-connect-cors-enabled-server-with-http – Anmol Jun 20 '15 at 17:16

1 Answers1

0

I had this problem while working through this tutorial, and cordova-plugin-whitelist solved it:

cordova plugin add cordova-plugin-whitelist

And this just above <title> in index.html:

<meta http-equiv="Content-Security-Policy" content="default-src 'unsafe-eval' 'self' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; connect-src http://my-server-name.com">

connect-src is the key. It "limits the origins to which you can connect (via XHR, WebSockets, and EventSource", according to this article.

  • I had this problem but instead of using the `` tag I included `` in the `config.xml` on Android (as states in the [docs](https://github.com/apache/cordova-plugin-whitelist)) – Lucas Lazaro Dec 08 '15 at 16:32