离子套接字设置不起作用

我使用MEAN.JS (NODEJS)为我的服务器端和客户端为我的门户网站,我有套接字安装实时更新和它的作品完美,当我在我的ionic framework基于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和IONIC控制器

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

}

问题是,当你创build一个新的套接字连接时,你没有指定你的套接字服务器的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创build一个套接字,因为cordova只是通过file协议来提供你的file ,你的socket.io会尝试通过相同的协议创build一个连接。

你的套接字服务器正在本地机器上运行,为了在你的android上工作,你必须指定你的机器的IP地址和服务器正在监听的端口。 去你的networking的喜好,并获得您的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是您的套接字服务器运行的端口。