Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable wss for ASP.NET Core Hot Reload

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?

like image 691
lonix Avatar asked Dec 06 '25 16:12

lonix


1 Answers

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.

Workaround 1

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.

Workaround 2

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.

like image 192
lonix Avatar answered Dec 09 '25 16:12

lonix