【问题标题】:How to enable wss in websockets in dotnetcore 2.0如何在 dotnet core 2.0 的 websocket 中启用 wss
【发布时间】:2018-11-22 01:10:04
【问题描述】:
我是 DotNet 的新手。我正在开发一个应用程序,它在 dot net core 2.0 中完成并使用 WebSockets。我已经关注this 来获取我的 WebSockets,它正在工作(使用 ws://)。
我想用 wss 保护它。我创建了一个自签名证书并修改了我的 URL 以使用 https,并且我的带有 https:// 的 REST 请求正在运行 [在不同的应用程序中]。当我遵循相同并将 URL 修改为 https 并尝试使用 wss:// 访问我的 WebSocket 时,它不起作用(未启用连接)。
我错过了什么吗?
【问题讨论】:
标签:
c#
.net
https
websocket
.net-core
【解决方案1】:
我猜你错过了端口,dotnet core https 正在使用与 http 不同的端口在你的客户端 js 文件中使用这个方法来获取正确的 url 只用你自己的路径替换 /ws 段
static GetBaseUrl(type) {
const wsScheme = document.location.protocol === "https:" ? "wss" : "ws";
const webScheme = document.location.protocol === "https:" ? "https" : "http";
const port = document.location.port ? (":" + document.location.port) : "";
switch (type) {
case Enums.UrlType.Web:
return webScheme + "://" + document.location.hostname + port;
case Enums.UrlType.WebSocket:
//wss is causing issue on the production
return wsScheme + "://" + document.location.hostname + port + "/ws/";
}
}