首页 > 解决方案 > 如何在 chrome websocket 代码中接收 TLS 握手错误

问题描述

https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent

1015    TLS Handshake   Reserved. Indicates that the connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified).

请参阅:浏览器兼容性
示例 chrome(版本 71.0)返回 1006 错误,firefox(版本 64.0(64 位))1015:

<script>
try{
    var websocket = new WebSocket("wss://self-signed.badssl.com/");

    websocket.onopen = function (event) {

    };
    websocket.onclose = function (event) {
        var reason;
        alert(event.code);

    };
    websocket.onmessage = function (event) {

    };
    websocket.onerror = function (event) {

    };
    }
    catch(err){
    document.getElementById("demo").innerHTML = err.message;
    }
</script>
<p id="demo"></p>

标签: javascripthtmlwebsocket

解决方案


catch 不能捕获错误,回调接收的是错误。您可以按照本指南


推荐阅读