mkuklis / phonegap-websocket

Websocket PhoneGap plugin for Android
203 stars 78 forks source link

Latest code on Cordova 2.x and Android 4.1.x #28

Closed MayurChandra closed 10 years ago

MayurChandra commented 10 years ago

Not sure if it is me but I just downloaded the latest code and it is failing to open the websocket on Android 4.1.x. It opens the socket on Android 2.x but not on Android 4.1.x

Is it just me? Or others having same problem? If I go back to older versions of phonegap-websocket.js it works on Android 4.1.x

mkuklis commented 10 years ago

@MayurChandra one thing which changed was the default draft (from 10 to 17). You can try to pass draft 10 to see if that makes any difference. Also can you share your log here? Thx.

MayurChandra commented 10 years ago

Hello @mkuklis, I tried to get a log but it is not even calling the code in phonegap-websocket.js file. Am using pg 2.5 and latest code from here. Will do some more debugging next few days. Seems like am the only one with this issue. Your sample code is for pg3. So wondering if something in my bit of code is dodgy. Will get back to you with more info/log. Thanks a lot. It is an excellent plugin.

mkuklis commented 10 years ago

@MayurChandra did you have a chance to look into it some more. Would be curious to learn move. Thanks! I'm asking because I'm using this plugin in one app and some people started reporting issues with connectivity but it works fine for me. I'm using PG 3.1

MayurChandra commented 10 years ago

@mkuklis, I just can't get any traces etc out of it, it is not even calling the ws constructor! If I go back to older versions of phonegap-websocket.js, it works fine with all other latest files checked in here. I am not using PG3.1. I am on PG2.5. Have you tried this code with PG2.x? I created a simple test app and had same results. Not sure what to do. Thanks.

dhuyvett commented 10 years ago

@MayurChandra Something odd is going on. I just tested the latest phonegap-2.x.x on Android 4.1.2 and it all worked fine. The only difference I can spot is that I'm using Cordova 2.6, but I wouldn't expect 2.5 to cause it to not work at all. More detail about what you are trying to do may be needed.

mkuklis commented 10 years ago

@MayurChandra your <access origin="..." /> is setup correctly right?

mkuklis commented 10 years ago

@MayurChandra there was a little typo in #29 which should be now fixed in 2.x.x branch. If you were using protocols that could cause the issue. If that doesn't help please provide a log even if you think it's not valuable I would be curious to see it.

pradeeguru commented 10 years ago

Star for plugin as it works for all my tested android versions.

When I used this plugin, I had an issue with some android 4.x versions as phone is getting some native socket thing. Then I removed "window.WebSocket || " at the beginning of phonegap-websocket.js file. then it starts working for all my 4.x phones too. Not sure, I have done something bad... but it works for all so far.. Thanks.

mkuklis commented 10 years ago

There has been a bug related to clobbers which was fixed yesterday: https://github.com/mkuklis/phonegap-websocket/commit/843672ba96be110c6f9df9aa2bb75c63e30ca80d You should be able to grab the newest version and it should work without removing window.WebSocket. Thx.

MayurChandra commented 10 years ago

@dhuyvett @mkuklis Hello. Thanks for getting back. Sorry was away few days. Yes, I have got it working just now. But I did exactly what @pradeeguru suggested and got it working. Now my phonegap-websocket.js start of file looks like this after commenting out like below. I have just tested it on Android 4.1.x, will test on Android 2.x now.

/ window.WebSocket || / (function() {

var websocketId = 0;

// Websocket constructor var WebSocket = window.WebSocket = function(url, protocols, options) {

dhuyvett commented 10 years ago

@MayurChandra are you sure that you are using the Cordova 2.x branch? Your original message included:

/*var exec = require('cordova/exec');

and that require is only in the master branch.

MayurChandra commented 10 years ago

@dhuyvett yes, I made a little mistake but I just checked and I am using the correct version of the code from here.

MayurChandra commented 10 years ago

@dhuyvett - OK downloaded the latest code from 2.x branch, deleted old files and did a clean build of the app. As soon as I put the comment as explained above, it works on 4.1.x. Thanks.

dhuyvett commented 10 years ago

please add the following just above the commented out section and let me know what you get:

console.log("window.WebSocket: " + window.WebSocket); console.log("window.WebSocket (stringified): " + JSON.stringify(window.WebSocket));

MayurChandra commented 10 years ago

11-21 15:05:40.902: D/CordovaLog(26246): window.WebSocket: function WebSocket() { [native code] } 11-21 15:05:40.902: I/Web Console(26246): window.WebSocket: function WebSocket() { [native code] }:1 11-21 15:05:40.902: D/CordovaLog(26246): window.WebSocket (stringified): undefined 11-21 15:05:40.902: I/Web Console(26246): window.WebSocket (stringified): undefined:2

dhuyvett commented 10 years ago

OK. That looks like Android 4.4. Is this a phone or an emulator? 4.1 should not have native WebSockets. What does your call to the WebSocket constructor look like?

MayurChandra commented 10 years ago

I am using Android v4.1.2 and it a real phone and not emulator.

connection = new WebSocket('wss://10.0.2.10:23005')

dhuyvett commented 10 years ago

Interesting … What phone and what provider? Rooted? Stock or custom mod?

MayurChandra commented 10 years ago

Samsung Galaxy S2. Not rooted and it is not using any mobile data. Only WiFi. The phone is unlocked i.e. I can put any provider's SIM card and it works. It is running Samsung's custom Android build. I have same results on other 4.x devices from different manufacturer etc.

mkuklis commented 10 years ago

@MayurChandra that is very strange indeed. function WebSocket() { [native code] } would indicate that your android version has a native WebSocket support. Could you please remove the plugin and see if you can get websocket connection?

MayurChandra commented 10 years ago

@mkuklis wow, that is interesting. I will try now. Hmm.

mkuklis commented 10 years ago

Guys I just ran into this :/ http://stackoverflow.com/questions/19368794/check-for-websocket-support-in-native-android-browser-in-a-cordova-project if google does it just to reserve the space that's just horrible.

MayurChandra commented 10 years ago

@mkuklis removed all refs to the plugin and it doesn't work at all. Removed all downloaded files from here, updated config.xml, did a clean build and it doesn't work i.e. socket doesn't open. Thanks

dhuyvett commented 10 years ago

It looks like the Samsung Galaxy S3 (at least) has a broken native implementation of WebSockets http://stackoverflow.com/questions/13349305/web-sockets-on-samsung-galaxy-s3-android-browser I may need to change the test for Android 4.4 or above rather than for WebSockets present :-(

mkuklis commented 10 years ago

Maybe we could improve the condition here just to be safe:


if (window.WebSocket && window.WebSocket.prototype.send) {
 return;
}
mkuklis commented 10 years ago

@MayurChandra thank you for reporting this issue! This is an important fix. Also could you please try to put the above condition at the beginning of the file and see what happens?

MayurChandra commented 10 years ago

Yes, just trying this @mkuklis just deleted the plugin and need to being it back, won't be long. Thanks.

MayurChandra commented 10 years ago

@mkuklis just tried this change and doesn't open socket. My file now looks like this

if (window.WebSocket && window.WebSocket.prototype.send) { return; }

window.WebSocket || (function() {

var websocketId = 0;

// Websocket constructor window.WebSocket = function(url, protocols, options) {

mkuklis commented 10 years ago

Thank you @MayurChandra. @dhuyvett I think we have to come up with a test which tests only for KitKat here. Maybe a smart regex with navigator.userAgent could work. I don't have access to KitKat here. I wonder if something like this would work.

if (navigator.userAgent.match(/Android 4\.4/i)) {
}
mkuklis commented 10 years ago

@MayurChandra @dhuyvett I'm going to add something similar to this:


function hasWebSocket() {
  var m = navigator.userAgent.match(/Android ([0-9]+)\.([0-9]+)/i);

  if (m) {
    var x = parseInt(m[1], 10);
    var y = parseInt(m[2], 10);

    return window.WebSocket && (x > 4 || (x == 4 && y >= 4));
  }

  return false;
}
MayurChandra commented 10 years ago

@mkuklis let me try and get back.

mkuklis commented 10 years ago

@MayurChandra this has been just added https://github.com/mkuklis/phonegap-websocket/commit/53dde61db0ad85a92b050bc10091e1ce753ec7ab Please give it a try and let me know if it works. Cheers!

MayurChandra commented 10 years ago

@mkuklis works! Checked on same exact device. Brilliant! Thank you. Checked on 2 devices and works great!

mkuklis commented 10 years ago

@MayurChandra thank you for testing. I'm glad it works for you!

MayurChandra commented 10 years ago

@mkuklis Thank you! Excellent plugin, I would give few more stars!