SignalR 与 ABP Angular 版本的集成

SignalR integration with ABP Angular version

我正在尝试将 SignalR 库与 ABP 应用程序(具有 Angular 版本的.NET Core 3.1)一起使用,但是当我来到官方文档中提到的最后一步时,我不知道我应该把代码放在哪里:

var chatHub = null;

abp.signalr.startConnection(abp.appPath + 'signalr-myChatHub', function (connection) {
    chatHub = connection; // Save a reference to the hub

    connection.on('getMessage', function (message) { // Register for incoming messages
        console.log('received message: ' + message);
    });
}).then(function (connection) {
    abp.log.debug('Connected to myChatHub server!');
    abp.event.trigger('myChatHub.connected');
});

abp.event.on('myChatHub.connected', function() { // Register for connect event
    chatHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
});

我不知道,我应该把上面的代码放在哪里?

您可以在 app.component.tsAppComponentngOnInit 中完成:

export class AppComponent extends AppComponentBase implements OnInit {
    ...

    ngOnInit(): void {
        ...

        // SignalRAspNetCoreHelper.initSignalR(); // Replace this line with the block below
        SignalRAspNetCoreHelper.initSignalR(() => {
            var chatHub = null;

            abp.signalr.startConnection(abp.appPath + 'signalr-myChatHub', function (connection) {
                chatHub = connection; // Save a reference to the hub

                connection.on('getMessage', function (message) { // Register for incoming messages
                    console.log('received message: ' + message);
                });
            }).then(function (connection) {
                abp.log.debug('Connected to myChatHub server!');
                abp.event.trigger('myChatHub.connected');
            });

            abp.event.on('myChatHub.connected', function() { // Register for connect event
                chatHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
            });
        });

        ...
    }

    ...
}