【问题标题】:Signal R in Aurelia frameworkAurelia 框架中的信号 R
【发布时间】:2015-11-16 23:06:30
【问题描述】:

我正在尝试将信号 r 集成到 Aurelia 客户端应用程序中。 我已经启动并运行了信号器服务器,它与 durandal 等其他框架一起运行。 我的 Hub 网址是这样的“http://localhost/signalr/signalr/hubs

在客户端添加信号器的步骤

  1. 在 index.html 中添加 jquery.signalR-{version}.js
  2. 在 index.html 中添加“http://localhost/signalr/signalr/hubs
  3. 在 app.js 中添加连接起始块
 $.connection.notificationHub.connection.start().done(
            function () {
                console.log('hurray, connection established');               
            }
            ).fail(function () {
                console.log('oops - connection failed');
            });

现在在运行应用程序时看到以下错误找不到未定义的notificationHub

请告诉我问题出在哪里。

【问题讨论】:

    标签: signalr-hub signalr.client aurelia


    【解决方案1】:

    我已经使用生成的代理将 SignalR 添加到 Aurelia 应用程序中,并且只是按照说明 on this page 手动执行。我创建了一个单独的模块,但我想你可以在 app.js 中使用它。您需要添加如下内容:

    ...    
    // Create a connection -> create a proxy -> attach event handlers -> start
    this.connection = $.hubConnection('http://my.signalr.host.here');
    this.hubProxy = this.connection.createHubProxy('myHubName');
    this.connection.logging = true;
    
    // Add method handler(s)
    this.hubProxy.on('myMethodCalledByTheServer', e => {
        console.log('SignalR called us', e);
    });
    
    // Connect to SignalR events if required. eg:
    this.connection.connectionSlow(() => {
        console.log('The SignalR connection is slow');
    });
    
    // Start
    this.connection.start()
        .then(c => {
            console.log('Started', c);
        });
    

    使用以下方法调用服务器方法:

    this.hubProxy.invoke('myServerMethod', args);
    

    在函数处理程序中,我们使用 Aurelia 事件聚合器将适当的消息发布到应用程序的其余部分。

    通过使用手动方法,您无需在 index.html 中包含 signalr/hubs 脚本。

    【讨论】:

    • @PranayVerma - 你有你的答案,所以标记它接受。这将使搜索引擎更容易找到。
    猜你喜欢
    • 2015-02-27
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多