javascript Android Stock 浏览器是否支持 WebSocket?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20379442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebSocket supported in Android Stock Browser or not?
提问by Ben Muircroft
using https://github.com/einaros/ws
使用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?
我要问为什么android默认浏览器打不开连接?
I visit www.websocket.org/echo.html on the default android browser and it says This browser supports websocket.so what is the problem?
我在默认的 android 浏览器上访问 www.websocket.org/echo.html,它说这个浏览器支持 websocket。那么问题是什么?
This simple code works on iphone safari, windows chrome, android mobile chrome no problem.
这个简单的代码适用于 iphone safari、windows chrome、android mobile chrome 没问题。
On android default browser I can also console.dir(window.WebSocket); and it shows the WebSocket Object no differently than other browsers.
在android默认浏览器上我也可以console.dir(window.WebSocket); 它显示的 WebSocket 对象与其他浏览器没有什么不同。
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]}
回答by gzost
The Android stock browser does not, in fact, support WebSocket.
Android 股票浏览器实际上不支持 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.
显然已经完成了一些工作以准备添加支持,因此浏览器中的 API 就在那里,即您可以创建一个 WebSocket 对象。只是这实际上并没有在幕后做任何事情。
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".
这会导致一个简单的功能支持检查,它只是尝试创建套接字对象,显示 WebSocket 支持。改为检查创建的 WebSocket 对象的 readyState,您将看到它永远不会从“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.
从 Android 4.4 开始,不再有股票浏览器。Web 视图组件已切换到 Android 版 Chrome - 这确实支持 WebSocket。