禁用 ASP.NET Core 热重载的 wss

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

我正在使用 ASP.NET Core 的热重载功能。它尝试连接两个 WebSockets:

ws
wss

ws连接成功,热重载工作。但我本地开发环境没有使用HTTPS,所以wss连接失败,devtools控制台总是显示:

Firefox can’t establish a connection to the server at wss://localhost:50005/.
WebSocket failed to connect.

这很烦人。我可以将热重载配置为仅使用

ws
吗?

asp.net-core .net-core hot-reload asp.net-core-7.0
1个回答
0
投票

官方似乎没有解决方案。我在存储库上添加了请求请投票

在那之前,这是我非常丑陋的解决方法(改编自this),它对热重载脚本进行猴子修补。

在加载热重载脚本 (

/_framework/aspnetcore-browser-refresh.js
) 之前运行此命令:

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);
};

现在 devtools 控制台仅记录:

undefined

仍然很烦人,但比两个红色错误的噪音要小得多。

如果您有更好的无噪音解决方法,请发布,我会接受您的答案。

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