SignalR与ABP Angular版本集成

问题描述 投票:0回答:1

我想在ABP应用(.NET Core 3.1 with Angular version)中使用SignalR库,但是当我到了官方文档中提到的最后一步时,我不知道应该把代码放在哪里。

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
});

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

signalr aspnetboilerplate .net-core-3.1 asp.net-boilerplate
1个回答
1
投票

你可以这样做 ngOnInitAppComponentapp.component.ts:

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
            });
        });

        ...
    }

    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.