Skip to content

Commit b4532ce

Browse files
committed
Add abp.signalr-client for Angular
1 parent e4439a7 commit b4532ce

File tree

7 files changed

+153
-3
lines changed

7 files changed

+153
-3
lines changed

angular/.angular-cli.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"glob": "abp.signalr.js",
1515
"input": "../node_modules/abp-web-resources/Abp/Framework/scripts/libs/",
1616
"output": "./assets/abp/"
17+
},
18+
{
19+
"glob": "abp.signalr-client.js",
20+
"input": "../node_modules/abp-web-resources/Abp/Framework/scripts/libs/",
21+
"output": "./assets/abp/"
1722
}
1823
],
1924
"index": "index.html",

angular/src/app/app.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AppConsts } from '@shared/AppConsts';
33
import { AppComponentBase } from '@shared/app-component-base';
44

55
import { SignalRHelper } from '@shared/helpers/SignalRHelper';
6+
import { SignalRAspNetCoreHelper } from '@shared/helpers/SignalRAspNetCoreHelper';
67

78
@Component({
89
templateUrl: './app.component.html'
@@ -19,7 +20,11 @@ export class AppComponent extends AppComponentBase implements OnInit, AfterViewI
1920

2021
ngOnInit(): void {
2122
if (this.appSession.application.features['SignalR']) {
22-
SignalRHelper.initSignalR();
23+
if (this.appSession.application.features['SignalR.AspNetCore']) {
24+
SignalRAspNetCoreHelper.initSignalR();
25+
} else {
26+
SignalRHelper.initSignalR();
27+
}
2328
}
2429

2530
abp.event.on('abp.notifications.received', userNotification => {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var abp = abp || {};
2+
(function () {
3+
4+
// Check if SignalR is defined
5+
if (!signalR) {
6+
return;
7+
}
8+
9+
// Create namespaces
10+
abp.signalr = abp.signalr || {};
11+
abp.signalr.hubs = abp.signalr.hubs || {};
12+
13+
// Configure the connection
14+
function configureConnection(connection) {
15+
// Set the common hub
16+
abp.signalr.hubs.common = connection;
17+
18+
// Reconnect if hub disconnects
19+
connection.onclose(function (e) {
20+
if (e) {
21+
abp.log.debug('Connection closed with error: ' + e);
22+
}
23+
else {
24+
abp.log.debug('Disconnected');
25+
}
26+
27+
if (!abp.signalr.autoConnect) {
28+
return;
29+
}
30+
31+
setTimeout(function () {
32+
if ($.connection.hub.state === $.signalR.connectionState.disconnected) {
33+
$.connection.hub.start();
34+
}
35+
}, 5000);
36+
});
37+
38+
// Register to get notifications
39+
connection.on('getNotification', function (notification) {
40+
abp.event.trigger('abp.notifications.received', notification);
41+
});
42+
}
43+
44+
// Connect to the server
45+
abp.signalr.connect = function() {
46+
var url = abp.signalr.url || '/signalr';
47+
48+
// Add query string: https://github.com/aspnet/SignalR/issues/680
49+
if (abp.signalr.qs) {
50+
url += '?' + abp.signalr.qs;
51+
}
52+
53+
// Start the connection.
54+
startConnection(url, configureConnection).then(function (connection) {
55+
abp.log.debug('Connected to SignalR server!'); //TODO: Remove log
56+
abp.event.trigger('abp.signalr.connected');
57+
// Call the Register method on the hub.
58+
connection.invoke('register').then(function () {
59+
abp.log.debug('Registered to the SignalR server!'); //TODO: Remove log
60+
});
61+
})
62+
.catch(error => {
63+
abp.log.debug(error.message);
64+
});
65+
};
66+
67+
// Starts a connection with transport fallback - if the connection cannot be started using
68+
// the webSockets transport the function will fallback to the serverSentEvents transport and
69+
// if this does not work it will try longPolling. If the connection cannot be started using
70+
// any of the available transports the function will return a rejected Promise.
71+
function startConnection(url, configureConnection) {
72+
return function start(transport) {
73+
abp.log.debug(`Starting connection using ${signalR.TransportType[transport]} transport`)
74+
var connection = new signalR.HubConnection(url, {transport: transport});
75+
if (configureConnection && typeof configureConnection === 'function') {
76+
configureConnection(connection);
77+
}
78+
79+
return connection.start()
80+
.then(function() {
81+
return connection;
82+
})
83+
.catch(function(error) {
84+
abp.log.debug(`Cannot start the connection use ${signalR.TransportType[transport]} transport. ${error.message}`);
85+
if (transport !== signalR.TransportType.LongPolling) {
86+
return start(transport + 1);
87+
}
88+
89+
return Promise.reject(error);
90+
});
91+
}(signalR.TransportType.WebSockets);
92+
}
93+
94+
abp.signalr.startConnection = startConnection;
95+
96+
if (abp.signalr.autoConnect === undefined) {
97+
abp.signalr.autoConnect = true;
98+
}
99+
100+
if (abp.signalr.autoConnect) {
101+
abp.signalr.connect();
102+
}
103+
104+
})();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { AppConsts } from '@shared/AppConsts';
2+
import { UtilsService } from '@abp/utils/utils.service';
3+
4+
export class SignalRAspNetCoreHelper {
5+
static initSignalR(): void {
6+
7+
var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);
8+
9+
abp.signalr = {
10+
autoConnect: true,
11+
connect: undefined,
12+
hubs: undefined,
13+
qs: AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken),
14+
url: AppConsts.remoteServiceBaseUrl + '/signalr'
15+
};
16+
17+
jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js');
18+
}
19+
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Sessions/SessionAppService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public async Task<GetCurrentLoginInformationsOutput> GetCurrentLoginInformations
1919
ReleaseDate = AppVersionHelper.ReleaseDate,
2020
Features = new Dictionary<string, bool>
2121
{
22-
{ "SignalR", SignalRFeature.IsAvailable }
22+
{ "SignalR", SignalRFeature.IsAvailable },
23+
{ "SignalR.AspNetCore", SignalRFeature.IsAspNetCore }
2324
}
2425
}
2526
};

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/AbpCompanyName.AbpProjectName.Web.Host.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</PropertyGroup>
1616

1717
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
18-
<DefineConstants>FEATURE_SIGNALR</DefineConstants>
18+
<DefineConstants>FEATURE_SIGNALR;FEATURE_SIGNALR_ASPNETCORE</DefineConstants>
1919
</PropertyGroup>
2020

2121
<ItemGroup>

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup/Startup.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using AbpCompanyName.AbpProjectName.Authentication.JwtBearer;
1515
using AbpCompanyName.AbpProjectName.Configuration;
1616
using AbpCompanyName.AbpProjectName.Identity;
17+
using AbpCompanyName.AbpProjectName.Web.Startup;
1718

1819
#if FEATURE_SIGNALR_OWIN
1920
using Microsoft.AspNet.SignalR;
@@ -23,6 +24,10 @@
2324
using AbpCompanyName.AbpProjectName.Owin;
2425
#endif
2526

27+
#if FEATURE_SIGNALR_ASPNETCORE
28+
using Abp.Web.SignalR.Hubs;
29+
#endif
30+
2631
namespace AbpCompanyName.AbpProjectName.Web.Host.Startup
2732
{
2833
public class Startup
@@ -46,6 +51,10 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
4651
IdentityRegistrar.Register(services);
4752
AuthConfigurer.Configure(services, _appConfiguration);
4853

54+
#if FEATURE_SIGNALR_ASPNETCORE
55+
services.AddSignalR();
56+
#endif
57+
4958
// Configure CORS for angular2 UI
5059
services.AddCors(
5160
options => options.AddPolicy(
@@ -109,6 +118,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
109118
app.UseAppBuilder(ConfigureOwinServices);
110119
#endif
111120

121+
#if FEATURE_SIGNALR_ASPNETCORE
122+
app.UseSignalR(routes =>
123+
{
124+
routes.MapHub<AbpCommonHub>("/signalr");
125+
});
126+
#endif
127+
112128
app.UseMvc(routes =>
113129
{
114130
routes.MapRoute(

0 commit comments

Comments
 (0)