首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SignalR与ABP角版的集成

SignalR与ABP角版的集成
EN

Stack Overflow用户
提问于 2020-06-03 16:06:50
回答 1查看 809关注 0票数 0

我试图在SignalR应用程序中使用.NET库(带有棱角版本的.NET Core3.1),但是当我谈到正式文档中提到的最后一步时,我不知道该把代码放在哪里:

代码语言:javascript
复制
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
});

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-03 16:32:18

你可以用ngOnInit of AppComponent in app.component.ts来做。

代码语言:javascript
复制
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
            });
        });

        ...
    }

    ...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62177445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档