繁体   English   中英

沉默网:: ERR_CONNECTION_REFUSED

[英]Silence net::ERR_CONNECTION_REFUSED

连接到不存在的Web套接字服务器会导致大量错误被记录到控制台,通常是... net::ERR_CONNECTION_REFUSED

任何人都有想法让hackaround沉默这个输出? XMLHttpRequest将无法工作,因为如果无法访问服务器,它会产生相同的详细错误输出。

这里的目标是测试服务器是否可用,如果它是连接到它,否则使用回退,并执行此操作而不会通过错误输出向控制台发送垃圾邮件。

Chrome本身正在发送这些消息,并且无法阻止它们。 这是如何构建铬的功能; 每当ResourceFetcher对象尝试获取资源时,其响应都会传递回其上下文,如果出现错误,浏览器会将其打印到控制台 - 请参阅此处

类似的问题可以在这里找到。

如果您愿意,可以使用chrome控制台过滤器,因为此问题讨论了在控制台中阻止这些错误,但无法以编程方式阻止消息。

我不知道你为什么要阻止这个错误输出。 我想你只是想在调试时摆脱它们。 所以我在这里提供的工作可能对调试很有用。

现场演示: http//blackmiaool.com/soa/43012334/boot.html

如何使用它?

打开演示页面,单击“启动”按钮,它将打开一个新选项卡。 单击新选项卡中的“测试”按钮,然后检查下面的结果。 如果您想获得积极的结果,请将网址更改为wss://echo.websocket.org

为什么?

通过使用发布消息 ,我们可以使浏览器选项卡相互通信。 因此,我们可以将这些错误输出移动到我们不关心的选项卡。

PS您可以自由刷新目标页面而不会丢失它与引导页面之间的连接。

PPS您还可以使用存储事件来实现此目的。

boot.html:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>boot page</title>

</head>

<body>
    <button onclick="boot()">boot</button>
    <p>BTW, you can boot the page without the button if you are willing to allow the "pop-up"</p>
    <script>
        var targetWindow;

        function init() {
            targetWindow
        }

        function boot() {
            targetWindow = window.open("target.html");
        }
        boot();
        window.addEventListener('message', function(e) {
            var msg = e.data;
            var {
                action,
                url,
                origin,
            } = msg;

            if (action === "testUrl") {
                let ws = new WebSocket(url);
                ws.addEventListener("error", function() {
                    targetWindow.postMessage({
                        action: "urlResult",
                        url,
                        data: false,
                    }, origin);
                    ws.close();
                });
                ws.addEventListener("open", function() {
                    targetWindow.postMessage({
                        action: "urlResult",
                        url,
                        data: true,
                    }, origin);
                    ws.close();
                });
            }


        });

    </script>
</body>

</html>

target.html上

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>target page</title>
</head>

<body>
    <h4>input the url you want to test:</h4>
    <textarea type="text" id="input" style="width:300px;height:100px;">
        </textarea>
    <br>
    <div>try <span style="color:red">wss://echo.websocket.org</span> for success result(may be slow)</div>
    <button onclick="test()">test</button>
    <div id="output"></div>
    <script>
        var origin = location.origin;
        var testUrl = origin.replace(/^https?/, "ws") + "/abcdef"; //not available of course
        document.querySelector("#input").value = testUrl;

        function output(val) {

            document.querySelector("#output").textContent = val;
        }

        function test() {
            if (window.opener) {
                window.opener.postMessage({
                    action: "testUrl",
                    url: document.querySelector("#input").value,
                    origin,
                }, origin);
            } else {
                alert("opener is not available");
            }

        }
        window.addEventListener('message', function(e) {
            var msg = e.data;
            if (msg.action === "urlResult") {
                output(`test ${msg.url} result: ${msg.data}`);
            }
        });

    </script>
</body>

</html>

暂无
暂无

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

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