简体   繁体   中英

Ionic Socket.io Build on android giving Transport Pollings Issue

I am building a game on Ionic using Socket.io for communicating plays between clients. I have tried connecting to an Express server running on local host as well as the same one on Digital Ocean:

var express   = require('express');
var app       = express();
var socketio  = require('socket.io');

var server    = app.listen(8080);
var io        = require('socket.io').listen(server);

io.sockets.on('connect', function(socket) {
});

It works fantastic with ionic serve, and when I emulate on iOS, howerver, when I run or emulate on Android it can't connect, giving me the following errors:

Failed to load resource: the server responded with a status of 404 (Not Found) http:// my server /socket.io/?EIO=3&transport=polling&t=1434658858975-0
Failed to load resource: the server responded with a status of 404 (Not Found) http:// my server /socket.io/?EIO=3&transport=polling&t=1434658860600-1
Failed to load resource: the server responded with a status of 404 (Not Found) http:// my server /socket.io/?EIO=3&transport=polling&t=1434658862535-2

I am connecting through a Factory

.factory('socket', function($rootScope) {
  var socket = io.connect( server );
  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);
          }
        });
      })
    }
  };
});

I am completely blank on this. Is there a setting somewhere that, if changed, would allow me to connect?

You could try building in MeteorJS which is a Node/Express based app framework that uses socket.io It has a really good Ionic package (full support) and Cordova support with built in OAuth as well. I've built a few mobile apps and it's a good stack. Check MeteorJS.com and packages at atmosphere.com

Don't know if you are still looking, but I may have come across a solution to this issue.

I was having the exact same problem where the app would work correctly on iOS but not on android. I was able to get it to work by listening not to the localhost but to the ip of my computer pointed to the same port.

Inside of the socket factory, I had been using var socket = io.connect('http://localhost:3000')

Instead try var socket = io.connect('http://192.168.12.3:3000') . or whatever your computer's ip address is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM