Home » Android » javascript – WebSocket supported in Android Stock Browser or not?

javascript – WebSocket supported in Android Stock Browser or not?

Posted by: admin June 15, 2020 Leave a comment

Questions:

using https://github.com/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.com: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.org/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]}
How to&Answers:

The 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 component has been switched to Chrome for Android – and this does support WebSocket.