Aurelia框架中的信号R.

Pra*_*rma 3 signalr-hub signalr.client aurelia aurelia-cli

我正在尝试将信号r集成到Aurelia客户端应用程序中.我已经启动并运行了signalr服务器,它正在与durandal等其他框架一起运行.我的中心URl就是这样的" 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');
            });
Run Code Online (Sandbox Code Playgroud)

现在运行应用程序跟踪错误,看不到undefined的notificationHub

请告诉我问题在哪里.

shu*_*nty 6

我已经将SignalR添加到Aurelia应用程序中,使用生成的代理,只是按照本页上的说明手动执行.我创建了一个单独的模块,但我想你可以在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);
    });
Run Code Online (Sandbox Code Playgroud)

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

this.hubProxy.invoke('myServerMethod', args);
Run Code Online (Sandbox Code Playgroud)

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

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