Skip to content

Commit c28cb51

Browse files
committed
Added signalR
1 parent 49ccbaf commit c28cb51

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

WebApi/Hubs/BroadcastHub.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.SignalR;
3+
4+
namespace Hubs.BroadcastHub
5+
{
6+
public class BroadcastHub : Hub
7+
{
8+
public async Task SendMessage(string message)
9+
{
10+
await Clients.All.SendAsync("send", message);
11+
}
12+
}
13+
}

WebApi/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using WebApi.Services;
3030
using WebApi.Repositories;
3131
using WebApi.Helpers;
32+
using Hubs.BroadcastHub;
3233

3334
namespace WebApi
3435
{
@@ -128,6 +129,7 @@ public void ConfigureServices(IServiceCollection services)
128129

129130
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
130131
services.AddCors();
132+
services.AddSignalR();
131133

132134
services.AddSwaggerGen(c =>
133135
{
@@ -204,6 +206,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IService
204206
app.UseDefaultFiles();
205207
app.UseStaticFiles();
206208

209+
app.UseSignalR(routes =>
210+
{
211+
routes.MapHub<BroadcastHub>("/broadcast");
212+
});
213+
207214
CreateUsersAndRoles(services).Wait();
208215
AttendanceConfiguration(services).Wait();
209216
}

WebApi/WebApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="3.2.0" />
1414
<PackageReference Include="Microsoft.AspNetCore.All" />
15+
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.2" />
1516
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.0" />
1617
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.5.0" />
1718
</ItemGroup>

WebClient/package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WebClient/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
"build": "node build/build.js"
1414
},
1515
"dependencies": {
16+
"@aspnet/signalr": "^1.0.2",
1617
"axios": "^0.18.0",
1718
"lodash": "^4.17.10",
1819
"moment": "^2.22.2",
1920
"nprogress": "^0.2.0",
21+
"tslib": "^1.9.3",
2022
"vue": "^2.5.2",
2123
"vue-notification": "^1.3.10",
2224
"vue-router": "^3.0.1",

WebClient/src/components/log-employee-form.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<script>
2525
import { LOG_EMPLOYEE } from '@/store/actions-type'
2626
import { mapGetters } from 'vuex'
27+
// import { HubConnection } from '@aspnet/signalr'
28+
import * as signalR from "@aspnet/signalr";
29+
2730
2831
export default {
2932
data () {
@@ -59,6 +62,18 @@ export default {
5962
resetForm () {
6063
this.$refs.form.reset()
6164
}
65+
},
66+
67+
mounted () {
68+
const connection = new signalR.HubConnectionBuilder()
69+
.withUrl("http://localhost:5000/broadcast")
70+
.build();
71+
72+
connection.on("send", (data) => {
73+
console.log(data)
74+
})
75+
76+
connection.start().catch(err => document.write(err));
6277
}
6378
}
6479
</script>

0 commit comments

Comments
 (0)