Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket supported in Android Stock Browser or not?

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]}
like image 368
Ben Muircroft Avatar asked Dec 04 '13 15:12

Ben Muircroft


People also ask

How do I know if a WebSocket is supported?

function check_ws_object() { try { var websocket = new WebSocket( "wss://echo.websocket.org" ); return true; } catch ( e ) { ; } return false; } // function check_support() { if ( !( WebSocket in window ) ) { if ( nll( window. WebSocket) ) { if ( ! this.

Is WebSocket widely supported?

Wide Support The WebSockets API has surprising support across browsers. According to caniuse, WebSockets are currently supported across every major browser, web-based and mobile, with 98% of globally used browsers supporting the interface.

How do I check if the browser is running Web page supports WebSockets?

Now using Chrome browser open the html file your created in the beginning. If your browser supports WebSocket(), then you would get alert indicating that your browser supports WebSocket and finally when you click on "Run WebSocket" you would get Goodbye message sent by the server script.

When can I use WebSockets?

When a client needs to react quickly to a change (especially one it cannot predict), a WebSocket may be best. Consider a chat application that allows multiple users to chat in real-time. If WebSockets are used, each user can both send and receive messages in real-time.


Video Answer


1 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.

like image 119
gzost Avatar answered Sep 29 '22 15:09

gzost