沉默网络::ERR_CONNECTION_REFUSED

问题描述 投票:0回答:4

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

... net::ERR_CONNECTION_REFUSED

有人有办法让这个输出保持沉默吗?

XMLHttpRequest
将不起作用,因为如果无法访问服务器,它会产生相同的详细错误输出。

这里的目标是测试服务器是否可用,如果可用则连接到它,否则使用后备,并且在不向控制台发送错误输出的情况下执行此操作。

javascript google-chrome firefox websocket safari
4个回答
20
投票

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

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

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


2
投票

我不知道为什么要阻止这个错误输出。我猜你只是想在调试时摆脱它们。因此,我在这里提供的解决方案可能仅对调试有用。

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

如何使用?

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

wss://echo.websocket.org

为什么?

通过使用post message,我们可以使浏览器选项卡相互通信。因此我们可以将这些错误输出移至我们不关心的选项卡。

附注您可以自由刷新目标页面,而不会失去它与启动页面之间的连接。

P.P.S 您还可以使用存储事件来实现这一点。

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>

目标.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>

0
投票

也可能是由于协议不匹配。就像尝试访问

https
访问
http
网站。


0
投票

我有两个应用程序,一个在 IIS 上本地部署,另一个在调试模式下运行。部署的应用程序需要其他应用程序进行身份验证。错误地,我只在浏览器中运行了已部署的应用程序,忘记在调试模式下本地运行其他应用程序。然后我就遇到了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.