Cordova App Socket.IO connection not working

北慕城南 提交于 2019-12-13 02:07:18

问题


I'm trying to set up my cordova app to use socket.io websocket connection but I can't get it to connect when I run in debug mode. Everything I've read tells me this should work, what am I doing wrong?

Server Side

var http = require('http');
var server = http.createServer();
var io = require('socket.io')(server);

server.listen(5022, function () {
    console.log('listening on *:5022');
});

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

App

document.addEventListener('deviceready',    
    function() {
        onDeviceReady.bind(this);
        console.log('Device is Ready')
        var socket = io.connect('http://MyDomain:5022');
        socket.on('connect', function () {
            socket.on('text', function (text) {
                console.log(text);
            });
        });
    },
false);

I get the Device is Ready message, but I get this message:

Failed to load resource: net::ERR_CONNECTION_REFUSED

File: xhr_proxy, Line: 0, Column: 0


回答1:


The problem was that I was using the Ripple Emulator in the browser. It has some crappy settings that restrict domain access. You can change it but I thought screw it and decided to use the Android Emulator to debug instead and it worked fine.



来源:https://stackoverflow.com/questions/28021519/cordova-app-socket-io-connection-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!