简体   繁体   中英

Cordova App Socket.IO connection not working

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM