22

Connecting to a non-existent web socket server results in loud errors being logged to the console, usually to the tune of ... net::ERR_CONNECTION_REFUSED.

Anyone have an idea for a hackaround to silence this output? XMLHttpRequest won't work since it yields the same verbose error output if the server is not reachable.

The goal here is to test if the server is available, if it is then connect to it, otherwise use a fallback, and to do this without spamming the console with error output.

Casper Beyer
  • 2,203
  • 2
  • 22
  • 35
  • @jfriend00 I'm afraid that won't work, chrome does not delegate to `console.error` to spew out the error. – Casper Beyer Mar 25 '17 at 04:18
  • This Q looks related: [How can I stop jQuery.ajax() from logging failures to the console?](http://stackoverflow.com/questions/11949095/how-can-i-stop-jquery-ajax-from-logging-failures-to-the-console). Suspect that nothing much has changed, and the answer is still "you can't" – James Thorpe Mar 27 '17 at 16:28
  • Is the ws server serving your html files? – blackmiaool Mar 28 '17 at 01:39
  • @blackmiaool in this particular case no but, this question is also about curiosity ;) – Casper Beyer Mar 28 '17 at 18:53
  • The answer is still that you cannot, but later versions of Chrome do have better filtering capabilities and they also label the source of the log more clearly. – Casper Beyer May 09 '18 at 03:03

3 Answers3

20

Chrome itself is emitting these messages, and there is no way to block them. This is a function of how chrome was built; whenever a ResourceFetcher object attempts to fetch a resource, its response is passed back to its context, and if there's an error, the browser prints it to the console - see here.

Similar question can be found here.

If you'd like, you can use a chrome console filter as this question discusses to block these errors in your console, but there is no way to programmatically block the messages.

Community
  • 1
  • 1
joepin
  • 1,418
  • 1
  • 16
  • 17
2

I don't know why do you want to prevent this error output. I guess you just want to get rid of them when debugging. So I provide a work around here may be just useful for debugging.

Live demo: http://blackmiaool.com/soa/43012334/boot.html

How to use it?

Open the demo page, click the "boot" button, it will open a new tab. Click the "test" button in the new tab and check the result below. If you want to get a positive result, change the url to wss://echo.websocket.org.

Why?

By using post message, we can make browser tabs communicate with each other. So we can move those error output to a tab that we don't concern.

P.S. You can refresh the target page freely without loosing the connection between it and boot page.

P.P.S You can also use storage event to achieve this.

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>
blackmiaool
  • 5,234
  • 2
  • 22
  • 39
  • As for the why, just because its a-lot of noise, and not wanting false 'ITS NOT WORKING' issues being constantly posted on GitHub. Latest versions of chrome did introduce filtering of network messages but it's broken for web socket errors. – Casper Beyer Mar 29 '17 at 01:36
  • @CasperBeyer I see. If you host the websocket together with html, you can start a websocket at the beginning of the page. This ws must connect successfully since you can get the html. And listen to its close event to know the time when the server is not available. Or just build a server to check if ws server available on html server. – blackmiaool Mar 29 '17 at 01:44
  • @CasperBeyer So this answer(not the comment) is not acceptable for you, right? – blackmiaool Mar 29 '17 at 01:45
  • @CasperBeyer Generally, you can put a pretty "boot" button in the center of the page. Page opened with onclick callback won't be blocked. http://stackoverflow.com/questions/2587677/avoid-browser-popup-blockers – blackmiaool Mar 29 '17 at 02:24
  • @CasperBeyer There's a similar solution: put a "test" button on your page. When user click it, open a small new window with `window.open` displaying "testing". After test finished, close the little window automatically, and determine what to do. If you think it's acceptable, I'll make a demo. – blackmiaool Mar 29 '17 at 13:42
  • The solution in answer is suit for you(developer). The solution above is suit for other users. – blackmiaool Mar 29 '17 at 13:43
0

Could be also due to protocol mis-match. like trying to reach https for http website.

sql_dummy
  • 715
  • 8
  • 23