Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Commit 711c0cc

Browse files
committed
initial creation
1 parent 1c4eb0c commit 711c0cc

12 files changed

+5137
-0
lines changed

Company.WebApplication1.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
</ItemGroup>
9+
10+
</Project>

Hubs/ChatHub.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.SignalR;
5+
6+
namespace AutoReconnectDemo
7+
{
8+
public class ChatHub : Hub
9+
{
10+
public Task SendMessageToGroup(string username, string groupName, string message)
11+
{
12+
return Clients.Group(groupName).SendAsync("Send", $"{username} ({groupName}): {message}");
13+
}
14+
15+
public async Task AddToGroup(string username, string groupName)
16+
{
17+
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
18+
await Clients.Group(groupName).SendAsync("Send", $"{username} has joined the group {groupName}.");
19+
}
20+
21+
public async Task RemoveFromGroup(string username, string groupName)
22+
{
23+
await Clients.Group(groupName).SendAsync("Send", $"{username} is leaving the group {groupName}.");
24+
await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
25+
}
26+
}
27+
}

Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace AutoReconnectDemo
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}

Startup.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
10+
11+
namespace AutoReconnectDemo
12+
{
13+
public class Startup
14+
{
15+
// This method gets called by the runtime. Use this method to add services to the container.
16+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
17+
public void ConfigureServices(IServiceCollection services)
18+
{
19+
services.AddSignalR();
20+
}
21+
22+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
23+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
24+
{
25+
if (env.IsDevelopment())
26+
{
27+
app.UseDeveloperExceptionPage();
28+
}
29+
30+
// These two lines can be removed if you are using Razor pages.
31+
app.UseDefaultFiles();
32+
app.UseStaticFiles();
33+
34+
app.UseRouting();
35+
app.UseEndpoints(builder =>
36+
{
37+
builder.MapHub<ChatHub>("/chat");
38+
});
39+
}
40+
}
41+
}

appsettings.Development.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}

appsettings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

readme.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SignalR Automatic Reconnection Demo
2+
3+
This demo provides easy way to show the new `withAutomaticReconnect`, `onreconnected`, and `onreconnecting` functionality resident in the 3.0 preview 6 release. Here are the steps to run the demo:
4+
5+
1. Clone this repository.
6+
7+
1. Open `wwwroot\js\chat.js`. The first few bits of code demonstrate how to enable automatic reconnection and how to handle events related to reconnects.
8+
9+
1. Speak to the new `withAutomaticReconnect` method, and make sure to point out that its behavior can be customized by providing an array of millisecond values the client should wait to reconnect.
10+
11+
```javascript
12+
var connection = new signalR.HubConnectionBuilder()
13+
.withUrl("/chat")
14+
.withAutomaticReconnect([0, 3000, 5000, 10000, 15000, 30000])
15+
//.withAutomaticReconnect([0, 2000, 10000, 30000]) yields the default behavior
16+
.build();
17+
```
18+
19+
1. Speak to the `onreconnecting` handler and how it enables developers to update their UI when the app is trying to reconnect.
20+
21+
```javascript
22+
connection.onreconnecting((error) => {
23+
disableUi(true);
24+
const li = document.createElement("li");
25+
li.textContent = `Connection lost due to error "${error}". Reconnecting.`;
26+
document.getElementById("messagesList").appendChild(li);
27+
});
28+
```
29+
30+
1. Speak to the `onreconnected` handler and how it gives developers a place to update their UI once the app has successfully reconnected.
31+
32+
```javascript
33+
connection.onreconnected((connectionId) => {
34+
disableUi(false);
35+
const li = document.createElement("li");
36+
li.textContent = `Connection reestablished. Connected.`;
37+
document.getElementById("messagesList").appendChild(li);
38+
});
39+
```
40+
41+
1. Mention that the docs are available for the preview at https://aka.ms/signalr/auto-reconnect.
42+
43+
1. Make sure to mention that the automatic reconnection is available today via `npm install @aspnet/signalr@next`.

wwwroot/css/site.css

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification\
2+
for details on configuring this project to bundle and minify static web assets. */
3+
4+
a.navbar-brand {
5+
white-space: normal;
6+
text-align: center;
7+
word-break: break-all;
8+
}
9+
10+
/* Provide sufficient contrast against white background */
11+
a {
12+
color: #0366d6;
13+
}
14+
15+
.btn-primary {
16+
color: #fff;
17+
background-color: #1b6ec2;
18+
border-color: #1861ac;
19+
}
20+
21+
.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
22+
color: #fff;
23+
background-color: #1b6ec2;
24+
border-color: #1861ac;
25+
}
26+
27+
/* Sticky footer styles
28+
-------------------------------------------------- */
29+
html {
30+
font-size: 14px;
31+
}
32+
@media (min-width: 768px) {
33+
html {
34+
font-size: 16px;
35+
}
36+
}
37+
38+
.container {
39+
max-width: 960px;
40+
}
41+
42+
.pricing-header {
43+
max-width: 700px;
44+
}
45+
46+
.border-top {
47+
border-top: 1px solid #e5e5e5;
48+
}
49+
.border-bottom {
50+
border-bottom: 1px solid #e5e5e5;
51+
}
52+
53+
.box-shadow {
54+
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
55+
}
56+
57+
button.accept-policy {
58+
font-size: 1rem;
59+
line-height: inherit;
60+
}
61+
62+
/* Sticky footer styles
63+
-------------------------------------------------- */
64+
html {
65+
position: relative;
66+
min-height: 100%;
67+
}
68+
69+
body {
70+
/* Margin bottom by footer height */
71+
margin-bottom: 60px;
72+
}
73+
.footer {
74+
position: absolute;
75+
bottom: 0;
76+
width: 100%;
77+
overflow: scroll;
78+
white-space: nowrap;
79+
line-height: 60px; /* Vertically center the text there */
80+
}

wwwroot/index.html

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Real-time Chat with SignalR</title>
7+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"
8+
crossorigin="anonymous"
9+
integrity="sha256-eSi1q2PG6J7g7ib17yAaWMcrr5GrtohYChqibrV7PBE="/>
10+
<link rel="stylesheet" href="/css/site.css" />
11+
</head>
12+
<body>
13+
14+
<header>
15+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
16+
<div class="container">
17+
<a class="nav-link text-dark" href="/index.html">Chat with SignalR</a>
18+
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
19+
aria-expanded="false" aria-label="Toggle navigation">
20+
<span class="navbar-toggler-icon"></span>
21+
</button>
22+
</div>
23+
</nav>
24+
</header>
25+
<div class="container">
26+
<main role="main" class="pb-3">
27+
<div class="container">
28+
<div class="row">&nbsp;</div>
29+
<div class="row">
30+
<div class="col-12">
31+
<form class="form-inline">
32+
<div class="input-prepend input-append">
33+
<input aria-label="user name" type="text" name="user-name" id="user-name" placeholder="Type your user name" />
34+
</div>
35+
</form>
36+
</div>
37+
</div>
38+
<div class="row">
39+
<div class="col-12">
40+
<form class="form-inline">
41+
<div class="input-prepend input-append">
42+
<input aria-label="group name" type="text" name="group-name" id="group-name" placeholder="Type a group name" />
43+
<input type="button" id="join-group" class="btn btn-sm btn-primary" value="Join Group" />
44+
<input type="button" id="leave-group" class="btn btn-sm btn-primary" value="Leave Group" />
45+
</div>
46+
</form>
47+
</div>
48+
</div>
49+
<div class="row">
50+
<div class="col-12">
51+
<form class="form-inline">
52+
<div class="input-prepend input-append">
53+
<input aria-label="group message" type="text" name="group-message" id="group-message-text" placeholder="Type a message" />
54+
<input type="button" id="groupmsg" class="btn btn-sm btn-primary" value="Send to Group" />
55+
</div>
56+
</form>
57+
</div>
58+
</div>
59+
<div class="row">
60+
<div class="col-12">
61+
<hr />
62+
</div>
63+
</div>
64+
<div class="row">
65+
<div class="col-6">&nbsp;</div>
66+
<div class="col-6">
67+
<ul id="messagesList"></ul>
68+
</div>
69+
</div>
70+
</div>
71+
</main>
72+
</div>
73+
74+
<footer class="border-top footer text-muted">
75+
<div class="container">
76+
&copy; copyrightYear - Company.WebApplication1
77+
</div>
78+
</footer>
79+
80+
<!-- jQuery is only being used here to enable advanced Bootstrap features.
81+
jQuery is not a requirement for ASP.NET Core SignalR. -->
82+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
83+
crossorigin="anonymous"
84+
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=">
85+
</script>
86+
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.min.js"
87+
crossorigin="anonymous"
88+
integrity="sha256-E/V4cWE4qvAeO5MOhjtGtqDzPndRO1LBk8lJ/PR7CA4=">
89+
</script>
90+
<script src="/js/signalr.js"></script>
91+
<script src="/js/site.js"></script>
92+
<script src="/js/chat.js"></script>
93+
94+
</body>
95+
</html>

0 commit comments

Comments
 (0)