简体   繁体   English

在Cordova项目中的本机android浏览器中检查WebSocket支持

[英]Check for WebSocket support in native android browser in a cordova project

I have made up a cordova project which will be intensively using websockets. 我已经完成了一个cordova项目,该项目将大量使用websockets。 I have set up a few java classes which will implement websockets to the android platform while still be able to use the websockets in javascript in the same way as a native implementation will. 我已经建立了一些Java类,这些类将在Android平台上实现websockets,同时仍然能够以与本机实现相同的方式在javascript中使用websockets。 That means, i then have a websocket object with its methods onopen, onmessage and so on... 这意味着,我然后有了一个websocket对象,其方法为onopen,onmessage等。

Since the mobile safari (iPhone) already supports websockets, i dont have to implement the javascript part again (its only 1 html/css/js source for both platforms). 由于移动浏览器(iPhone)已支持websocket,因此我不必再次实现javascript部分(两个平台都只有1个html / css / js源)。 That means i have to write a function which tells me if android is supported. 这意味着我必须编写一个告诉我是否支持android的函数。

What i have: 我有的:

var supportsWebSockets = function() {
    if ('WebSocket' in window) {
        if (WebSocket.hasOwnProperty('onopen'))
            return true;
        else
            return false;
    } else {
        return false;
    }
}

But that doesnt do it. 但这没有做到。 I then made a script which will output all methods and properties of the WebSocket Object: 然后,我制作了一个脚本,该脚本将输出WebSocket对象的所有方法和属性:

var obj = new WebSocket('ws://echo.websocket.org');
var methods = [];
for (var m in obj) {
    methods.push(m);
}

document.write(methods.join("<br>"));

That outputs the same properties and methods in android as in mobile safari. 这会在android中输出与移动Safari中相同的属性和方法。

I guess that means, they have reserved the namespaces with empty functions. 我想这意味着,他们保留了带有空函数的名称空间。

How will i then be able to check if WebSockets are supported or not? 然后如何检查WebSockets是否受支持? I dont want to use a User Agent string to identify, since i dont want to modify it again when new version with eventual support are coming. 我不想使用用户代理字符串来进行标识,因为当最终支持的新版本问世时,我不想再次对其进行修改。

Weird nobody encountered this problem before.. 以前没人会遇到这个问题。

Any thoughts? 有什么想法吗?

Ok so I can see that there are comments with slightly varying correct answers, I personally like to check positively, so would do the following to check if a browser supports WebSockets using JavaScript... 好的,所以我可以看到有些注释的正确答案略有不同,我个人很想肯定地进行检查,因此将执行以下操作来检查浏览器是否支持使用JavaScript的WebSockets ...

if (typeof WebSocket === "function"){
    // implement WebSockets
} else {
    // fallback
}

The other answer would give a false negative in Safari. 另一个答案将在Safari中给出​​否定的答案。 I think the best approach is a combination of three things: 我认为最好的方法是结合三件事:

if ('WebSocket' in window
    && (typeof WebSocket === 'function' || typeof WebSocket === 'object')) {
    // supports WebSocket
} else {
    // does not support WebSocket
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM