I'm using ASP.NET Core's hot reload feature. It tries to connect with two websockets: ws and wss.
The ws connection succeeds and hot reload works. But I'm not using HTTPS in my local development environment, so the wss connection fails and the devtools console always shows:
Firefox can’t establish a connection to the server at wss://localhost:50005/.
WebSocket failed to connect.
That is very annoying. Can I configure hot reload to use ws only?
There doesn't seem to be an official solution. I added a request on the repo: PLEASE UPVOTE it.
Until then, here are very ugly workarounds which monkey-patch the hot reload script.
Run them before the hot reload script (/_framework/aspnetcore-browser-refresh.js) is loaded.
class DummyWebSocket {
constructor(url, protocol) { }
addEventListener(eventName, callback) {
if (eventName === 'close')
callback(new ErrorEvent(''));
}
removeEventListener(eventName, callback) { }
}
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
if (args[0].startsWith('wss://localhost:'))
return new DummyWebSocket(...args);
else
return new nativeWebSocket(...args);
};
Now the devtools console only logs:
undefined
Still annoying, but significantly less noisy than two red errors. Note that this can/will fail when the underlying script is changed, so it's a temporary solution.
This logs nothing at all, but it's very aggressive as it monkey-patches console.debug.
class DummyWebSocket {
constructor(url, protocol) { }
addEventListener(eventName, callback) {
if (eventName === 'close')
callback(); // <----
}
removeEventListener(eventName, callback) { }
}
const nativeWebSocket = window.WebSocket;
window.WebSocket = function(...args){
if (args[0].startsWith('wss://localhost:'))
return new DummyWebSocket(...args);
else
return new nativeWebSocket(...args);
};
const nativeConsoleDebug = window.console.debug; // <----
window.console.debug = function(...data) {
if (data[0] === 'WebSocket failed to connect.')
return;
else
nativeConsoleDebug(...data);
}
If you have a better workaround, please post it and I'll accept your answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With