英文:
How can I configure Blazor to use different base URLs for HTTP and WebSocket connections?
问题
I have a Blazor-Server app running on a corporate intranet, and user's Browsers are connecting to server through our standard web-app portal which does few things, such as providing a friendly URL and takes care of the SSO, failover... Unfortunately this web portal has some limitation on WebSocket messaging, limiting the payload to 64kb, which is breaking the SignalR communication as it happens to have the server sending few hundreds kb or even few Mb messages to the browser when the page contains a lot of data (like a big grid). It's working totally fine when I connect directly but I'm losing the SSO, which is a big loss in terms of user experience.
So here is my question: how can I continue to use the portal with a friendly CNAME for HTTP and tell Blazor to use the real Hostname for the WebSocket, for example:
- HTTP => http://myportal/myapp/home
- WebSocket => ws://server1234:6400/_blazor
I saw some JS code like below to allow changing the path, but I didn't find how to map the base URL...
<script>
(function start() {
Blazor.start({
configureSignalR: builder => builder.withUrl("/_barfoo")
});
})()
</script>
=== [Edit] Full solution based on Jason's answer ===
Browser code
// in _layout.cshtml
<script>
(function start() {
var signalUrl = 'http://server1234:6400/_blazor';
Blazor.start({
configureSignalR: builder => builder.withUrl(signalUrl)
});
})()
</script>
Server Code for CORS
// in Program.cs
builder.services.AddCors(options =>
{
options.AddPolicy("SigRPolicy", builder =>
builder.WithOrigins("http://server1234")
.AllowAnyMethod()
.AllowAnyHeader())
.SetIsOriginAllowed(_ => true);
// !! After app.UseRoutine (order matters)
app.UseCors("SigRPolicy");
// !! before app.UseAuthentication
});
But now I need to dynamically retrieve hostname:port with an API call.
英文:
I have a Blazor-Server app running on a corporate intranet, and user's Browsers are connecting to server through our standard web-app portal which does few things, such as providing a friendly URL and takes care of the SSO, failover... Unfortunately this web portal has some limitation on WebSocket messaging, limiting the payload to 64kb, which is breaking the SignalR communication as it happen to have the the server sending few hundreds kb or even few Mb messages to browser when the page contains a lot of data (like big grid).
It's working totally fine when I connect directly but I'm loosing the SSO which is a big loss in term of user experience.
So here is my question : how can I continue to use the portal with friendly CNAME for HTTP and tell Blazor to use the real Hostname for the WebSocket, ex :
- HTTP => http://myportal/myapp/home
- WebSocket ws://server1234:6400/_blazor
I saw some JS code like below to allow to change the path, but didn't find how to map the base url...
<script>
(function start() {
Blazor.start({
configureSignalR: builder => builder.withUrl("/_barfoo")
});
})()
</script>
=== [Edit] Full solution based on Jason answer ===
Browser code
// in _layout.cshtml
<script>
(function start() {
var signalUrl = 'http://server1234:6400/_blazor'
Blazor.start({
configureSignalR: builder => builder.withUrl(signalUrl)
});
})()
</script>
Server Code for CORS
// in Program.cs
builder.services.AddCors(options =>
{
options.AddPolicy("SigRPolicy", builder =>
builder.WithOrigin("http://server1234")
.AllowAnyMethod()
.AllowAnyHeader())
.SetIsOriginAllowed(_ => true);
// !! After app.UseRoutine (order matters)
app.UseCors("SigRPolicy")
// !! before app.UseAuthentication
});
But now I need to dynamically retrieve hostname:port with api call
答案1
得分: 1
你可以尝试将以下代码放入 _Host.cshtml
文件中:
<script>
(function start() {
var hostname = window.location.hostname; // 获取主机名
var signalRUrl = 'ws://' + hostname + ':6400/_blazor';
Blazor.start({
configureSignalR: builder => builder.withUrl(signalRUrl)
});
})();
</script>
我们需要确保在你的服务器上可以访问端口 6400。
英文:
You can try to using below code inside _Host.cshtml
file.
<script>
(function start() {
var hostname = window.location.hostname; // Get the hostname
var signalRUrl = 'ws://' + hostname + ':6400/_blazor';
Blazor.start({
configureSignalR: builder => builder.withUrl(signalRUrl)
});
})();
</script>
We need to make sure the 6400 port can be accessed in your server.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论