using
Server:
var WebSocketServer=require('ws').Server,wss=new WebSocketServer({port:8004});
wss.on('connection',function(s) {
s.on('message',function(_){console.log('received: '+_);});
});
Client:
var s=new WebSocket('ws://mysite:8004');
//android default browser dies here <---------------?
s.onopen=function(){
$('body').css({'background':'green'});
s.send('hi');
};
I have to ask why android default browser does not open the connection?
I visit www.websocket/echo.html on the default android browser and it says This browser supports websocket. so what is the problem?
This simple code works on iphone safari, windows chrome, android mobile chrome no problem.
On android default browser I can also console.dir(window.WebSocket); and it shows the WebSocket Object no differently than other browsers.
If someone knows why, please tell.
Thanks
UPDATE
if (!window.WebSocket && window.MozWebSocket) {
window.WebSocket = window.MozWebSocket;
alert('MozWebSocket');
}
else if (!window.WebSocket) {
alert("WebSocket not supported by this browser");
}
else{
alert('wtf!? '+window.WebSocket);
}
This gives me a console log of:
wtf!? function WebSocket(){[native code]}
using https://github./einaros/ws
Server:
var WebSocketServer=require('ws').Server,wss=new WebSocketServer({port:8004});
wss.on('connection',function(s) {
s.on('message',function(_){console.log('received: '+_);});
});
Client:
var s=new WebSocket('ws://mysite.:8004');
//android default browser dies here <---------------?
s.onopen=function(){
$('body').css({'background':'green'});
s.send('hi');
};
I have to ask why android default browser does not open the connection?
I visit www.websocket/echo.html on the default android browser and it says This browser supports websocket. so what is the problem?
This simple code works on iphone safari, windows chrome, android mobile chrome no problem.
On android default browser I can also console.dir(window.WebSocket); and it shows the WebSocket Object no differently than other browsers.
If someone knows why, please tell.
Thanks
UPDATE
if (!window.WebSocket && window.MozWebSocket) {
window.WebSocket = window.MozWebSocket;
alert('MozWebSocket');
}
else if (!window.WebSocket) {
alert("WebSocket not supported by this browser");
}
else{
alert('wtf!? '+window.WebSocket);
}
This gives me a console log of:
wtf!? function WebSocket(){[native code]}
Share
Improve this question
edited May 10, 2014 at 18:45
kelunik
6,9282 gold badges43 silver badges72 bronze badges
asked Dec 4, 2013 at 15:33
Ben MuircroftBen Muircroft
3,0348 gold badges44 silver badges71 bronze badges
5
-
What version of Android? On my 2.3 device, I see
window.WebSocket
asundefined
. Maybe whatever page you were testing on has aWebSocket
polyfill? – apsillers Commented Dec 4, 2013 at 15:42 - Android version 4.1.2 – Ben Muircroft Commented Dec 4, 2013 at 15:49
-
If
Websocket
is indeed defined in your browser, maybe it's failing because uses an older protocol. (Also, caniuse./websockets indicates that WebSockets came to the Android default browser in the 4.4 release. If you do haveWebSocket
in a non-4.4 browser, maybe that table is a reflection of a lack of modern WS protocol support.) – apsillers Commented Dec 4, 2013 at 15:50 - just tried ws protocol 8 but no change – Ben Muircroft Commented Dec 4, 2013 at 16:13
- Your stated browser (Android Browser 4.1.2), and your stated results, are at odds. Hit whatsmyuseragent. and include the User Agent of your problematic browser in your question. – Joakim Erdfelt Commented Dec 5, 2013 at 13:13
1 Answer
Reset to default 15The Android stock browser does not, in fact, support WebSocket.
Some work was apparently done in preparation for adding support, so the API in the browser is there, i.e. you can create a WebSocket object. It's just that this doesn't actually do anything behind the scenes.
This results in a simple feature support check, which just attempts to create the socket object, showing WebSocket support. Check the readyState for a created WebSocket object instead, and you'll see that this never changes from "0".
Starting with Android 4.4, there is no stock browser anymore. The Web view ponent has been switched to Chrome for Android - and this does support WebSocket.