我已经为 Socket.IO 连接设置了一个小的节点应用:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log('socket connected');
socket.on('disconnect', function () {
console.log('socket disconnected');
});
socket.emit('text', 'wow. such event. very real time.');
});
server.listen(3000, function() {
console.log('Socket.io Running');
});
在我的Ionic应用中,我有以下内容:
$ionicPlatform.ready(function(device) {
var socket = io('http://192.168.1.9:3000');
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
alert('here');
$state.go('signin');
});
当Ionic应用程序启动时,我会收到警报提示,这样我就知道
platform.ready
事件正在触发。但是Ionic应用程序从未连接到服务器上的套接字会话。如果我使用Chrome检查Ionic应用程序,则会记录以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883896073-0
Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883899867-1
Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883901481-2
Failed to load resource: the server responded with a status of 404 (Not Found) http://192.168.1.9:3000/socket.io/?EIO=3&transport=polling&t=1443883904606-3
但是IP地址和端口号是正确的。
为什么Ionic不能连接到Socket.iO?
编辑-04/10/2015
我的Ionic项目正在使用cordova-plugin-whitelist插件,我已经在我的index.html中添加了一个Content Security Policy 标签。
编辑05/10/2015
我也尝试过删除并重新添加Android平台,但仍然无法工作。