ABP 中的多个 SignalR 连接
Multiple SignalR connections in ABP
我有一个项目正在使用 ASP.NET 零模板。我已成功将 SignalR 集成到我的解决方案中,并且实时通知工作正常。我想要的是在我的解决方案中添加另一个集线器或扩展现有的 SignalR 集线器以添加更多集线器。
在SignalR AspNetCore Integration文档中,它说要将以下内容添加到Startup.cs文件中:
app.UseSignalR(routes =>
{
routes.MapHub<AbpCommonHub>("/signalr"); // default hub
routes.MapHub<HitchNotification.HitchHub>("/hitchHub"); // my hub
});
然而,问题是我需要建立连接的客户端!在 SignalRAspNetCoreHelper.ts 中,它设置 URL 使用 '/signalr'
集线器(默认集线器)。
export class SignalRAspNetCoreHelper {
static initSignalR(): void {
var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);
abp.signalr = {
autoConnect: true,
connect: undefined,
hubs: undefined,
qs: AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken),
url: AppConsts.remoteServiceBaseUrl + '/signalr'
};
jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js');
}
}
如果我将 '/signalr'
更改为 '/hitchHub'
,它可以正常工作。但我想要两者都在我的应用程序中!我试图为我自己的集线器创建一个类似于 SignalRAspNetCoreHelper.ts 的助手,并在 app.component.ts 中初始化它:
ngOnInit(): void {
if (this.appSession.application && this.appSession.application.features['SignalR']) {
if (this.appSession.application.features['SignalR.AspNetCore']) {
SignalRAspNetCoreHelper.initSignalR();
HitchHubHelper.initHitchHub();
}
}
}
但是 abp.signalr
似乎不能有多个连接到不同的集线器。
所以,基本上我有 2 个问题:
有什么方法可以将我自己的集线器功能添加到默认 AbpCommonHub
?这样,我可以简单地修改 abp.signalr-client.js 文件。
如果以上不可能,我如何在 abp.signalr
上设置多个集线器以便在我的应用程序中的任何地方访问?
- Is there any way that I could add my own hub's functions to the existing default AbpCommonHub? that way I could simply modify the abp.signalr-client file
当然可以。继承AbpCommonHub
:
public class HitchHub: AbpCommonHub
{
// Ctor omitted for brevity
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
}
更换集线器:
// routes.MapHub<AbpCommonHub>("/signalr");
routes.MapHub<HitchHub>("/signalr");
- If the above is impossible, how can I have multiple hubs on the abp.signalr to be accessible anywhere within my application?
以上并非不可能,但我还是会回答这个问题以演示多个集线器(对于 Angular)。
继承AbpHubBase
:
public class HitchHub : AbpHubBase, ITransientDependency
{
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
}
地图中心:
routes.MapHub<AbpCommonHub>("/signalr"); // No change
routes.MapHub<HitchHub>("/signalr-hitchHub"); // Prefix with '/signalr'
用法
这需要 Abp.AspNetCore.SignalR
v3.5.0-preview3
.
修改SignalRAspNetCoreHelper.ts:
abp.signalr = {
autoConnect: true, // No change
connect: undefined, // No change
hubs: undefined, // No change
qs: AppConsts.authorization.encrptedAuthTokenName + "=" + ... // No change
remoteServiceBaseUrl: AppConsts.remoteServiceBaseUrl, // Add this
startConnection: undefined, // Add this
url: '/signalr' // Changed from: AppConsts.remoteServiceBaseUrl + '/signalr'
};
// Modify the following block
jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js', () => {
var hitchHub;
abp.signalr.startConnection('/signalr-hitchHub', function (connection) {
hitchHub = 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 hitchHub server!');
abp.event.trigger('hitchHub.connected');
});
abp.event.on('hitchHub.connected', function() { // Register for connect event
hitchHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
});
});
我有一个项目正在使用 ASP.NET 零模板。我已成功将 SignalR 集成到我的解决方案中,并且实时通知工作正常。我想要的是在我的解决方案中添加另一个集线器或扩展现有的 SignalR 集线器以添加更多集线器。
在SignalR AspNetCore Integration文档中,它说要将以下内容添加到Startup.cs文件中:
app.UseSignalR(routes =>
{
routes.MapHub<AbpCommonHub>("/signalr"); // default hub
routes.MapHub<HitchNotification.HitchHub>("/hitchHub"); // my hub
});
然而,问题是我需要建立连接的客户端!在 SignalRAspNetCoreHelper.ts 中,它设置 URL 使用 '/signalr'
集线器(默认集线器)。
export class SignalRAspNetCoreHelper {
static initSignalR(): void {
var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);
abp.signalr = {
autoConnect: true,
connect: undefined,
hubs: undefined,
qs: AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken),
url: AppConsts.remoteServiceBaseUrl + '/signalr'
};
jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js');
}
}
如果我将 '/signalr'
更改为 '/hitchHub'
,它可以正常工作。但我想要两者都在我的应用程序中!我试图为我自己的集线器创建一个类似于 SignalRAspNetCoreHelper.ts 的助手,并在 app.component.ts 中初始化它:
ngOnInit(): void {
if (this.appSession.application && this.appSession.application.features['SignalR']) {
if (this.appSession.application.features['SignalR.AspNetCore']) {
SignalRAspNetCoreHelper.initSignalR();
HitchHubHelper.initHitchHub();
}
}
}
但是 abp.signalr
似乎不能有多个连接到不同的集线器。
所以,基本上我有 2 个问题:
有什么方法可以将我自己的集线器功能添加到默认
AbpCommonHub
?这样,我可以简单地修改 abp.signalr-client.js 文件。如果以上不可能,我如何在
abp.signalr
上设置多个集线器以便在我的应用程序中的任何地方访问?
- Is there any way that I could add my own hub's functions to the existing default AbpCommonHub? that way I could simply modify the abp.signalr-client file
当然可以。继承AbpCommonHub
:
public class HitchHub: AbpCommonHub
{
// Ctor omitted for brevity
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
}
更换集线器:
// routes.MapHub<AbpCommonHub>("/signalr");
routes.MapHub<HitchHub>("/signalr");
- If the above is impossible, how can I have multiple hubs on the abp.signalr to be accessible anywhere within my application?
以上并非不可能,但我还是会回答这个问题以演示多个集线器(对于 Angular)。
继承AbpHubBase
:
public class HitchHub : AbpHubBase, ITransientDependency
{
public async Task SendMessage(string message)
{
await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
}
地图中心:
routes.MapHub<AbpCommonHub>("/signalr"); // No change
routes.MapHub<HitchHub>("/signalr-hitchHub"); // Prefix with '/signalr'
用法
这需要 Abp.AspNetCore.SignalR
v3.5.0-preview3
.
修改SignalRAspNetCoreHelper.ts:
abp.signalr = {
autoConnect: true, // No change
connect: undefined, // No change
hubs: undefined, // No change
qs: AppConsts.authorization.encrptedAuthTokenName + "=" + ... // No change
remoteServiceBaseUrl: AppConsts.remoteServiceBaseUrl, // Add this
startConnection: undefined, // Add this
url: '/signalr' // Changed from: AppConsts.remoteServiceBaseUrl + '/signalr'
};
// Modify the following block
jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js', () => {
var hitchHub;
abp.signalr.startConnection('/signalr-hitchHub', function (connection) {
hitchHub = 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 hitchHub server!');
abp.event.trigger('hitchHub.connected');
});
abp.event.on('hitchHub.connected', function() { // Register for connect event
hitchHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
});
});