Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket on Android 4.4 with Phonegap

I'm working on an app which needs Websocket to communicate with my server. I'm working with Phonegap so I can run all the code first of all in my browser. Since Android 4.4 Websockets got native support in Android so it should work... I've implemented the Websocket with this code:

$(document).ready(function () {
    console.log('websocketready');
    startwebsocket();
});
var ws;

function startwebsocket() {
    ws = new WebSocket('ws://192.168.1.131:8080/.....');

    ws.onopen = function () {
        console.log("Websocket Ready!!");
    }
    ws.onclose = function () {
        console.log("Websocket Closed!!");
    }
    ws.onerror = function () {
        console.log("Websocket Error!!");
    }
    ws.onmessage = function (data) {
        console.log('getvalue : ' + data.data);
    }
}

function sendMessage(temp) {
    ws.send(temp);
}

This is just working fine in my browser (Chrome and firefox). But if I start the app with Phonegap on my Nexus 5 with android 4.4.2 I'm getting : 'WebSocket connection to 'ws://192.168.1.131:8080/.....' failed: Unexpected response code: 403'

Do you have any suggestions what I could have missed to do or what I did wrong?

like image 254
TobiasW Avatar asked May 02 '14 12:05

TobiasW


1 Answers

Do you provide 192.168.1.131 as authorised origin to cordova ?

Maybe you should try to add that to your config.xml :

<access origin="192.168.1.131" />

Take a look here for more informations.

Hope that help :)

like image 67
sarlam Avatar answered Oct 12 '22 06:10

sarlam