23

我正在使用 SignalR 2.0。在我的 VS 2012 上本地运行时一切正常。但是当我在 IIS 上发布站点时,它会中断。该站点已加载,但其中一个脚本返回 404 Not Found。脚本有点像。

https://example.com/signalr/negotiate?xxx

这条路确实不存在。正确的路径应该是:

https://example.com /private /signalr/negotiate?xxx

注意粗体部分。

在网站 ( https://example.com/ ) 内,我有另一个应用程序 ( https://example.com/private/ )。这个正在使用 SignalR。

这似乎是 SignalR 中的一个错误,因为可以从我的私人站点访问信号器/集线器路径。

4

8 回答 8

22

我有一个类似的问题。这是配置/signalr URL的文档。

但是,我的解决方案与文档不同。我没有更改标准app.MapSignalR(),而是更改了我的客户端代码以使用/MyApp/signalr. 这是“MyApp”是我的 Web 应用程序的虚拟目录的代码。

        var connection = $.hubConnection('/MyApp/signalr', {useDefaultPath: false});
        var changesHub = connection.createHubProxy('changesHub');

        changesHub.on('userCountChanged', function (count) {
            $('#user-count').text(count);
        });

        connection.start().done(function () {
            console.log('Hub has started');
            changesHub.invoke('subscribeToChanges', user.id);
        });

我尝试了另一种方法(将 MapSignalR 更改为 /signalr 路径),但这不起作用,协商仍然路由到 /MyApp/signalr/negotiate。

于 2014-05-16T01:53:04.320 回答
6

我遇到了同样的问题,应用程序在 IIS 默认网站中运行。
所有 Microsoft 示例都显示带有开头的集线器 url \,我复制了这些示例。但这意味着signalr路由来自默认网站而不是应用程序。删除领先\解决了它。

所以我在 Startup.cs 中使用了端点,例如:

endpoints.MapHub<MyHub>("myHub");

和 Javascript 中的集线器连接,例如:

var connection = new signalR.HubConnectionBuilder().withUrl("myHub").build();
于 2020-10-08T22:54:21.743 回答
3

有同样的问题。作为根站点的虚拟目录运行的网站。出于某种原因,在 ../signalr 中以 ../ 为前缀不起作用,但 ./signalr 可以。

我的示例代码:

function initSR() {
    // logs signalr messages
    $.connection.hub.logging = true;

    // Declare a proxy to reference the hub.             
    var chat = $.connection.myHub;
    $.connection.hub.url = "./signalr";
    $.connection.hub.start();
    // Create a function that the hub can call to broadcast messages.
    chat.client.broadcastMessage = function (message) {
        // Process Message, take action upon receipt
        alert(message);
    };
}
于 2017-01-03T19:42:09.263 回答
3

当带有信号器的网站未作为根站点运行时,我遇到了同样的问题。以下解决方案对我有用。而不是使用/signalr,使用../signalr。它适用于任何站点名称文件夹。没有硬编码名称“MyApp” var connection = $.hubConnection('../signalr', {useDefaultPath: false});

于 2015-11-15T23:43:57.160 回答
2

我遇到了同样的问题。我犯的错误是我调用了错误的端点 url,就像我在配置服务中映射 Signal Url 一样/notification,但调用[API-Host]/api/notification. 删除apifrom url 并[API-Host]/notification为我修复调用。

于 2020-09-01T03:32:00.027 回答
1

我有同样的问题,都是关于,你应该在 Startup.csCORS的配置中添加主机 URL,如下所示:CORS

            services.AddCors(option =>
        {
            option.AddPolicy("AutomationCors", builder =>
            {
                builder.AllowAnyMethod()
                    .AllowAnyHeader()
                    .WithOrigins("YOUR LOCALHOST URL",
                                 "YOUR HOST URL")
                    .AllowCredentials();
            });
        });
于 2020-01-24T07:19:54.420 回答
0

可能您在应用程序(https://example.com/private/ )中添加了 MapSignalR( ) 。如果您希望它在根目录上,请在您的网站上进行配置(https://example.com/

于 2013-11-11T18:12:17.197 回答
0

@styfle 为我指明了正确的方向,问题可以通过注入 BASE_URL 的更灵活的方式解决(至少在角度 4 中)

import { Injectable, Inject } from '@angular/core';
import { HubConnection } from '@microsoft/signalr';
import * as signalR from '@microsoft/signalr';
import { Subject, Observable } from 'rxjs';

@Injectable()
export class SignalRService {
    private hubConnection: HubConnection;
    private message: Subject<any>;

    constructor(@Inject('BASE_URL') private baseUrl: string) {
    }

    public connect() {
        this.message = new Subject<any>();
        this.hubConnection = new signalR.HubConnectionBuilder()
            .withUrl(this.baseUrl+"notification-hub")
            .withAutomaticReconnect()
            .build();
        // ...
    }
}
于 2020-05-10T12:35:02.027 回答