【问题标题】:Ionic Socket setup not working离子插座设置不起作用
【发布时间】:2016-03-11 20:12:15
【问题描述】:

我将MEAN.JS(NODEJS) 用于我的服务器端和客户端用于我的门户网站,当我在基于 ionic 框架的 android 应用程序中使用相同的控制器时,我设置了用于实时更新的套接字,并且它工作得很好吐出这个错误,
GET http://localhost:8100/socket.io/?EIO=3&transport=polling&t=LDaenOH 404 (Not Found)
我的服务器在http://localhost:3000上运行

我的 MEANJS 和 IONIC 套接字服务

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 和离子控制器

.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);
}

}

【问题讨论】:

  • 您的 Android 手机是否出现该错误?
  • 当我在我的系统上运行它时,它给出了上述错误,在我的 android 中我收到这样一个错误POST http://file/socket.io/?EIO=3&transport=polling&t=LDbYmfc net::ERR_EMPTY_RESPONSE
  • 我觉得我的 ionic socket 没有连接到我的服务器,我不知道如何继续这样做

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


【解决方案1】:

问题是当你创建一个新的套接字连接时,你没有为你的套接字服务器指定 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 是您的 socket 服务器运行的端口

【讨论】:

    猜你喜欢
    • 2017-10-27
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多