离子插座设置无法正常工作

时间:2016-03-11 11:44:36

标签: angularjs node.js sockets ionic-framework meanjs

我使用MEAN.JS(NODEJS)作为我的服务器端和客户端作为我的门户网站,我有实时更新的套接字设置,它的工作完美,当我在基于android的离子框架中使用相同的控制器应用程序会发出此错误,GET http://localhost:8100/socket.io/?EIO=3&transport=polling&t=LDaenOH 404 (Not Found)。我的服务器在http://localhost:3000

上运行

我的MEANJS和IONIC Socket服务

angular.module('core').service('Socket', ['Authentication', '$state', '$timeout',
function (Authentication, $state, $timeout) {
    // Connect to Socket.io server
    this.connect = function () {
      // Connect only when authenticated
      if (Authentication.user) {
        this.socket = io();
      }
    };
    this.connect();

    // Wrap the Socket.io 'on' method
    this.on = function (eventName, callback) {
      if (this.socket) {
        this.socket.on(eventName, function (data) {
          $timeout(function () {
            callback(data);
          });
        });
      }
    };

    // Wrap the Socket.io 'emit' method
    this.emit = function (eventName, data) {
      if (this.socket) {
        this.socket.emit(eventName, data);
      }
    };

    // Wrap the Socket.io 'removeListener' method
    this.removeListener = function (eventName) {
      if (this.socket) {
        this.socket.removeListener(eventName);
      }
    };
  }
]);

MEANJS和IONIC CONTROLLER

.controller('OrdersController', OrdersController);
OrdersController.$inject = ['$scope', '$state', '$timeout', 'orderResolve','OrdersService', 'Authentication', 'Socket'];

  function OrdersController($scope, $state, $timeout, order, OrdersService, Authentication, Socket) {
    var vm = this;

vm.order = order;
//vm.isNew = vm.order._id;
vm.authentication = Authentication;
vm.user = vm.authentication.user;
vm.error = null;
vm.message = null;
vm.form = {};
vm.remove = remove;
vm.save = save;
vm.saveUsingSocketEvents = saveUsingSocketEvents;

// Make sure the Socket is connected
if (!Socket.socket && Authentication.user) {
  Socket.connect();
}

Socket.on('orderUpdateError', function (response) {
  vm.error = response.message;
  //TODO: Use ng-messages
});

Socket.on('orderUpdateSuccess', function (response) {
  if (vm.order && vm.order._id.toString() === response.data._id) {
    vm.order = response.data;
    vm.message = response.message + ' by ' + (response.updatedBy !== vm.user.displayName ? response.updatedBy : 'You') + ' at ' + response.updatedAt;
  }
});
// Create new Order using SocketIO events
function saveUsingSocketEvents(isValid) {
  vm.error = null;

  if (!isValid) {
    $scope.$broadcast('show-errors-check-validity', 'orderForm');
    return false;
  }

  var order = new OrdersService({
        name: this.name,
        phone: this.phone
    });

  // we can send the user back to the orders list already
  // TODO: move create/update logic to service
  if (vm.order._id) {
    vm.order.$update(successCallback, errorCallback);
  } else {
    vm.order.$save(successCallback, errorCallback);
  }

  function successCallback(res) {
    $state.go('orders.view', {
      orderId: res._id
    });
  }

  function errorCallback(res) {
    vm.error = res.data.message;
  }

  // wait to send create request so we can create a smooth transition
  $timeout(function () {
    // TODO: move create/update logic to service
    if (vm.order._id) {
      Socket.emit('orderUpdate', vm.order);
    } else {
      Socket.emit('orderCreate', vm.order);
    }        
  }, 2000);
}

}

1 个答案:

答案 0 :(得分:1)

问题在于,当您创建新的套接字连接时,您没有为Socket服务器指定URL

angular.module('core').service('Socket', ['Authentication', '$state', '$timeout',
function (Authentication, $state, $timeout) {
    // Connect to Socket.io server
    this.connect = function () {
      // Connect only when authenticated
      if (Authentication.user) {
        this.socket = io(); //you haven't specified the url for your socket server
      }
    };

所以在你的Android应用程序上,它将尝试根据你当前的url创建一个套接字,因为cordova只是通过file协议提供你的文件,你的socket.io将尝试通过相同的协议创建一个连接

您的套接字服务器正在您的本地计算机上运行,​​要在您的Android上运行,您必须指定计算机的IP地址和服务器正在侦听的端口。转到您网络的首选项并获取您的IP地址并将其添加到您的套接字初始化以及服务器正在侦听的端口:

angular.module('core').service('Socket', ['Authentication', '$state', '$timeout',
function (Authentication, $state, $timeout) {
    // Connect to Socket.io server
    this.connect = function () {
      // Connect only when authenticated
      if (Authentication.user) {
        this.socket = io('http://192.1.0.123:8100');
      }
    };

192.1.0.123计算机的IP地址8100套接字服务器正在运行的端口