Home:ALL Converter>Silence net::ERR_CONNECTION_REFUSED

Silence net::ERR_CONNECTION_REFUSED

Ask Time:2017-03-25T11:52:24         Author:Casper Beyer

Json Formatter

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.

Author:Casper Beyer,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43012334/silence-neterr-connection-refused
joepin :

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.\n\nSimilar question can be found here.\n\nIf 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.",
2017-03-27T21:15:26
blackmiaool :

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.\n\nLive demo: http://blackmiaool.com/soa/43012334/boot.html\n\nHow to use it?\n\nOpen 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. \n\nWhy?\n\nBy 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.\n\nP.S. You can refresh the target page freely without loosing the connection between it and boot page.\n\nP.P.S You can also use storage event to achieve this.\n\nboot.html:\n\n<!DOCTYPE html>\n<html>\n\n<head>\n <meta charset=\"utf-8\" />\n <title>boot page</title>\n\n</head>\n\n<body>\n <button onclick=\"boot()\">boot</button>\n <p>BTW, you can boot the page without the button if you are willing to allow the \"pop-up\"</p>\n <script>\n var targetWindow;\n\n function init() {\n targetWindow\n }\n\n function boot() {\n targetWindow = window.open(\"target.html\");\n }\n boot();\n window.addEventListener('message', function(e) {\n var msg = e.data;\n var {\n action,\n url,\n origin,\n } = msg;\n\n if (action === \"testUrl\") {\n let ws = new WebSocket(url);\n ws.addEventListener(\"error\", function() {\n targetWindow.postMessage({\n action: \"urlResult\",\n url,\n data: false,\n }, origin);\n ws.close();\n });\n ws.addEventListener(\"open\", function() {\n targetWindow.postMessage({\n action: \"urlResult\",\n url,\n data: true,\n }, origin);\n ws.close();\n });\n }\n\n\n });\n\n </script>\n</body>\n\n</html>\n\n\ntarget.html\n\n<!DOCTYPE html>\n<html>\n\n<head>\n <meta charset=\"utf-8\" />\n <title>target page</title>\n</head>\n\n<body>\n <h4>input the url you want to test:</h4>\n <textarea type=\"text\" id=\"input\" style=\"width:300px;height:100px;\">\n </textarea>\n <br>\n <div>try <span style=\"color:red\">wss://echo.websocket.org</span> for success result(may be slow)</div>\n <button onclick=\"test()\">test</button>\n <div id=\"output\"></div>\n <script>\n var origin = location.origin;\n var testUrl = origin.replace(/^https?/, \"ws\") + \"/abcdef\"; //not available of course\n document.querySelector(\"#input\").value = testUrl;\n\n function output(val) {\n\n document.querySelector(\"#output\").textContent = val;\n }\n\n function test() {\n if (window.opener) {\n window.opener.postMessage({\n action: \"testUrl\",\n url: document.querySelector(\"#input\").value,\n origin,\n }, origin);\n } else {\n alert(\"opener is not available\");\n }\n\n }\n window.addEventListener('message', function(e) {\n var msg = e.data;\n if (msg.action === \"urlResult\") {\n output(`test ${msg.url} result: ${msg.data}`);\n }\n });\n\n </script>\n</body>\n\n</html>\n",
2017-03-28T05:40:55
yy