Skip to content

Commit c898560

Browse files
committed
Add abp.signalr-client
1 parent 534e204 commit c898560

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

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

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

1818
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
19-
<DefineConstants>FEATURE_SIGNALR</DefineConstants>
19+
<DefineConstants>FEATURE_SIGNALR;FEATURE_SIGNALR_ASPNETCORE</DefineConstants>
2020
</PropertyGroup>
2121

2222
<ItemGroup>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
using AbpCompanyName.AbpProjectName.Owin;
2020
#endif
2121

22+
#if FEATURE_SIGNALR_ASPNETCORE
23+
using Abp.Web.SignalR.Hubs;
24+
#endif
25+
2226
namespace AbpCompanyName.AbpProjectName.Web.Startup
2327
{
2428
public class Startup
@@ -42,6 +46,10 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
4246

4347
services.AddScoped<IWebResourceManager, WebResourceManager>();
4448

49+
#if FEATURE_SIGNALR_ASPNETCORE
50+
services.AddSignalR();
51+
#endif
52+
4553
// Configure Abp and Dependency Injection
4654
return services.AddAbp<AbpProjectNameWebMvcModule>(
4755
// Configure Log4Net logging
@@ -75,6 +83,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
7583
app.UseAppBuilder(ConfigureOwinServices);
7684
#endif
7785

86+
#if FEATURE_SIGNALR_ASPNETCORE
87+
app.UseSignalR(routes =>
88+
{
89+
routes.MapHub<AbpCommonHub>("/abpCommonHub");
90+
});
91+
#endif
92+
7893
app.UseMvc(routes =>
7994
{
8095
routes.MapRoute(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
// Start the connection.
47+
startConnection('/abpCommonHub', configureConnection).then(function (connection) {
48+
abp.log.debug('Connected to SignalR server!'); //TODO: Remove log
49+
abp.event.trigger('abp.signalr.connected');
50+
// Call the Register method on the hub.
51+
connection.invoke('register').then(function () {
52+
abp.log.debug('Registered to the SignalR server!'); //TODO: Remove log
53+
});
54+
})
55+
.catch(error => {
56+
abp.log.debug(error.message);
57+
});
58+
};
59+
60+
// Starts a connection with transport fallback - if the connection cannot be started using
61+
// the webSockets transport the function will fallback to the serverSentEvents transport and
62+
// if this does not work it will try longPolling. If the connection cannot be started using
63+
// any of the available transports the function will return a rejected Promise.
64+
function startConnection(url, configureConnection) {
65+
return function start(transport) {
66+
abp.log.debug(`Starting connection using ${signalR.TransportType[transport]} transport`)
67+
var connection = new signalR.HubConnection(url, {transport: transport});
68+
if (configureConnection && typeof configureConnection === 'function') {
69+
configureConnection(connection);
70+
}
71+
72+
return connection.start()
73+
.then(function() {
74+
return connection;
75+
})
76+
.catch(function(error) {
77+
abp.log.debug(`Cannot start the connection use ${signalR.TransportType[transport]} transport. ${error.message}`);
78+
if (transport !== signalR.TransportType.LongPolling) {
79+
return start(transport + 1);
80+
}
81+
82+
return Promise.reject(error);
83+
});
84+
}(signalR.TransportType.WebSockets);
85+
}
86+
87+
if (abp.signalr.autoConnect === undefined) {
88+
abp.signalr.autoConnect = true;
89+
}
90+
91+
if (abp.signalr.autoConnect) {
92+
abp.signalr.connect();
93+
}
94+
95+
})();

0 commit comments

Comments
 (0)