├── .gitignore
├── Company.WebApplication1.csproj
├── Hubs
└── ChatHub.cs
├── Program.cs
├── Startup.cs
├── appsettings.Development.json
├── appsettings.json
├── media
├── connect.png
├── disconnected.png
└── reconnected.png
├── readme.md
└── wwwroot
├── css
└── site.css
├── index.html
└── js
├── chat.js
├── signalr.js
└── site.js
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015/2017 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # Visual Studio 2017 auto generated files
33 | Generated\ Files/
34 |
35 | # MSTest test Results
36 | [Tt]est[Rr]esult*/
37 | [Bb]uild[Ll]og.*
38 |
39 | # NUNIT
40 | *.VisualState.xml
41 | TestResult.xml
42 |
43 | # Build Results of an ATL Project
44 | [Dd]ebugPS/
45 | [Rr]eleasePS/
46 | dlldata.c
47 |
48 | # Benchmark Results
49 | BenchmarkDotNet.Artifacts/
50 |
51 | # .NET Core
52 | project.lock.json
53 | project.fragment.lock.json
54 | artifacts/
55 | **/Properties/launchSettings.json
56 |
57 | # StyleCop
58 | StyleCopReport.xml
59 |
60 | # Files built by Visual Studio
61 | *_i.c
62 | *_p.c
63 | *_i.h
64 | *.ilk
65 | *.meta
66 | *.obj
67 | *.iobj
68 | *.pch
69 | *.pdb
70 | *.ipdb
71 | *.pgc
72 | *.pgd
73 | *.rsp
74 | *.sbr
75 | *.tlb
76 | *.tli
77 | *.tlh
78 | *.tmp
79 | *.tmp_proj
80 | *.log
81 | *.vspscc
82 | *.vssscc
83 | .builds
84 | *.pidb
85 | *.svclog
86 | *.scc
87 |
88 | # Chutzpah Test files
89 | _Chutzpah*
90 |
91 | # Visual C++ cache files
92 | ipch/
93 | *.aps
94 | *.ncb
95 | *.opendb
96 | *.opensdf
97 | *.sdf
98 | *.cachefile
99 | *.VC.db
100 | *.VC.VC.opendb
101 |
102 | # Visual Studio profiler
103 | *.psess
104 | *.vsp
105 | *.vspx
106 | *.sap
107 |
108 | # Visual Studio Trace Files
109 | *.e2e
110 |
111 | # TFS 2012 Local Workspace
112 | $tf/
113 |
114 | # Guidance Automation Toolkit
115 | *.gpState
116 |
117 | # ReSharper is a .NET coding add-in
118 | _ReSharper*/
119 | *.[Rr]e[Ss]harper
120 | *.DotSettings.user
121 |
122 | # JustCode is a .NET coding add-in
123 | .JustCode
124 |
125 | # TeamCity is a build add-in
126 | _TeamCity*
127 |
128 | # DotCover is a Code Coverage Tool
129 | *.dotCover
130 |
131 | # AxoCover is a Code Coverage Tool
132 | .axoCover/*
133 | !.axoCover/settings.json
134 |
135 | # Visual Studio code coverage results
136 | *.coverage
137 | *.coveragexml
138 |
139 | # NCrunch
140 | _NCrunch_*
141 | .*crunch*.local.xml
142 | nCrunchTemp_*
143 |
144 | # MightyMoose
145 | *.mm.*
146 | AutoTest.Net/
147 |
148 | # Web workbench (sass)
149 | .sass-cache/
150 |
151 | # Installshield output folder
152 | [Ee]xpress/
153 |
154 | # DocProject is a documentation generator add-in
155 | DocProject/buildhelp/
156 | DocProject/Help/*.HxT
157 | DocProject/Help/*.HxC
158 | DocProject/Help/*.hhc
159 | DocProject/Help/*.hhk
160 | DocProject/Help/*.hhp
161 | DocProject/Help/Html2
162 | DocProject/Help/html
163 |
164 | # Click-Once directory
165 | publish/
166 |
167 | # Publish Web Output
168 | *.[Pp]ublish.xml
169 | *.azurePubxml
170 | # Note: Comment the next line if you want to checkin your web deploy settings,
171 | # but database connection strings (with potential passwords) will be unencrypted
172 | *.pubxml
173 | *.publishproj
174 |
175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
176 | # checkin your Azure Web App publish settings, but sensitive information contained
177 | # in these scripts will be unencrypted
178 | PublishScripts/
179 |
180 | # NuGet Packages
181 | *.nupkg
182 | # The packages folder can be ignored because of Package Restore
183 | **/[Pp]ackages/*
184 | # except build/, which is used as an MSBuild target.
185 | !**/[Pp]ackages/build/
186 | # Uncomment if necessary however generally it will be regenerated when needed
187 | #!**/[Pp]ackages/repositories.config
188 | # NuGet v3's project.json files produces more ignorable files
189 | *.nuget.props
190 | *.nuget.targets
191 |
192 | # Microsoft Azure Build Output
193 | csx/
194 | *.build.csdef
195 |
196 | # Microsoft Azure Emulator
197 | ecf/
198 | rcf/
199 |
200 | # Windows Store app package directories and files
201 | AppPackages/
202 | BundleArtifacts/
203 | Package.StoreAssociation.xml
204 | _pkginfo.txt
205 | *.appx
206 |
207 | # Visual Studio cache files
208 | # files ending in .cache can be ignored
209 | *.[Cc]ache
210 | # but keep track of directories ending in .cache
211 | !*.[Cc]ache/
212 |
213 | # Others
214 | ClientBin/
215 | ~$*
216 | *~
217 | *.dbmdl
218 | *.dbproj.schemaview
219 | *.jfm
220 | *.pfx
221 | *.publishsettings
222 | orleans.codegen.cs
223 |
224 | # Including strong name files can present a security risk
225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
226 | #*.snk
227 |
228 | # Since there are multiple workflows, uncomment next line to ignore bower_components
229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
230 | #bower_components/
231 |
232 | # RIA/Silverlight projects
233 | Generated_Code/
234 |
235 | # Backup & report files from converting an old project file
236 | # to a newer Visual Studio version. Backup files are not needed,
237 | # because we have git ;-)
238 | _UpgradeReport_Files/
239 | Backup*/
240 | UpgradeLog*.XML
241 | UpgradeLog*.htm
242 | ServiceFabricBackup/
243 | *.rptproj.bak
244 |
245 | # SQL Server files
246 | *.mdf
247 | *.ldf
248 | *.ndf
249 |
250 | # Business Intelligence projects
251 | *.rdl.data
252 | *.bim.layout
253 | *.bim_*.settings
254 | *.rptproj.rsuser
255 |
256 | # Microsoft Fakes
257 | FakesAssemblies/
258 |
259 | # GhostDoc plugin setting file
260 | *.GhostDoc.xml
261 |
262 | # Node.js Tools for Visual Studio
263 | .ntvs_analysis.dat
264 | node_modules/
265 |
266 | # Visual Studio 6 build log
267 | *.plg
268 |
269 | # Visual Studio 6 workspace options file
270 | *.opt
271 |
272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
273 | *.vbw
274 |
275 | # Visual Studio LightSwitch build output
276 | **/*.HTMLClient/GeneratedArtifacts
277 | **/*.DesktopClient/GeneratedArtifacts
278 | **/*.DesktopClient/ModelManifest.xml
279 | **/*.Server/GeneratedArtifacts
280 | **/*.Server/ModelManifest.xml
281 | _Pvt_Extensions
282 |
283 | # Paket dependency manager
284 | .paket/paket.exe
285 | paket-files/
286 |
287 | # FAKE - F# Make
288 | .fake/
289 |
290 | # JetBrains Rider
291 | .idea/
292 | *.sln.iml
293 |
294 | # CodeRush
295 | .cr/
296 |
297 | # Python Tools for Visual Studio (PTVS)
298 | __pycache__/
299 | *.pyc
300 |
301 | # Cake - Uncomment if you are using it
302 | # tools/**
303 | # !tools/packages.config
304 |
305 | # Tabs Studio
306 | *.tss
307 |
308 | # Telerik's JustMock configuration file
309 | *.jmconfig
310 |
311 | # BizTalk build output
312 | *.btp.cs
313 | *.btm.cs
314 | *.odx.cs
315 | *.xsd.cs
316 |
317 | # OpenCover UI analysis results
318 | OpenCover/
319 |
320 | # Azure Stream Analytics local run output
321 | ASALocalRun/
322 |
323 | # MSBuild Binary and Structured Log
324 | *.binlog
325 |
326 | # NVidia Nsight GPU debugger configuration file
327 | *.nvuser
328 |
329 | # MFractors (Xamarin productivity tool) working folder
330 | .mfractor/
331 |
--------------------------------------------------------------------------------
/Company.WebApplication1.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Hubs/ChatHub.cs:
--------------------------------------------------------------------------------
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 | }
28 |
--------------------------------------------------------------------------------
/Program.cs:
--------------------------------------------------------------------------------
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();
24 | });
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Startup.cs:
--------------------------------------------------------------------------------
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("/chat");
38 | });
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "AllowedHosts": "*"
10 | }
11 |
--------------------------------------------------------------------------------
/media/connect.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SignalR/sample-WithAutomaticReconnect/2f275dc121ec29a37bd60315ee5e0546ea0ff01f/media/connect.png
--------------------------------------------------------------------------------
/media/disconnected.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SignalR/sample-WithAutomaticReconnect/2f275dc121ec29a37bd60315ee5e0546ea0ff01f/media/disconnected.png
--------------------------------------------------------------------------------
/media/reconnected.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SignalR/sample-WithAutomaticReconnect/2f275dc121ec29a37bd60315ee5e0546ea0ff01f/media/reconnected.png
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | We are no longer taking changes to this repository. It exists as an archived reference only.
2 |
3 | # SignalR Automatic Reconnection Demo
4 |
5 | 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:
6 |
7 | 1. Clone this repository.
8 |
9 | 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.
10 |
11 | 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.
12 |
13 | ```javascript
14 | var connection = new signalR.HubConnectionBuilder()
15 | .withUrl("/chat")
16 | .withAutomaticReconnect([0, 3000, 5000, 10000, 15000, 30000])
17 | //.withAutomaticReconnect([0, 2000, 10000, 30000]) yields the default behavior
18 | .build();
19 | ```
20 |
21 | 1. Speak to the `onreconnecting` handler and how it enables developers to update their UI when the app is trying to reconnect.
22 |
23 | ```javascript
24 | connection.onreconnecting((error) => {
25 | disableUi(true);
26 | const li = document.createElement("li");
27 | li.textContent = `Connection lost due to error "${error}". Reconnecting.`;
28 | document.getElementById("messagesList").appendChild(li);
29 | });
30 | ```
31 |
32 | 1. Speak to the `onreconnected` handler and how it gives developers a place to update their UI once the app has successfully reconnected.
33 |
34 | ```javascript
35 | connection.onreconnected((connectionId) => {
36 | disableUi(false);
37 | const li = document.createElement("li");
38 | li.textContent = `Connection reestablished. Connected.`;
39 | document.getElementById("messagesList").appendChild(li);
40 | });
41 | ```
42 |
43 | 1. Make sure to mention that the automatic reconnection is available today via `npm install @aspnet/signalr@next`.
44 |
45 | 1. Run `dotnet run` in the directory with the app.
46 |
47 | 1. Open the site in the browser at https://localhost:5001.
48 |
49 | 1. Open the debugging tools and call out how the connection has been established.
50 |
51 | 
52 |
53 | 1. Stop the app from running (either via `Ctrl-C` or stop the debugger). Flip back to the browser to show how the client has been disconnected.
54 |
55 | 
56 |
57 | 1. Restart the app and flip back to the browser window - don't refresh it - and wait until the client reconnects. Note, you may need to tune your values passed to `withAutomaticReconnect` depending on your own pace.
58 |
59 | 
60 |
61 | 1. Mention that the docs are available for the preview at https://aka.ms/signalr/auto-reconnect.
62 |
63 |
--------------------------------------------------------------------------------
/wwwroot/css/site.css:
--------------------------------------------------------------------------------
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:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Real-time Chat with SignalR
7 |
10 |
11 |
12 |
13 |
14 |
25 |
26 |
27 |
28 |
29 |
38 |
49 |
59 |
64 |
70 |
71 |
72 |
73 |
74 |
79 |
80 |
82 |
86 |
90 |
91 |
92 |
93 |
94 |
95 |
--------------------------------------------------------------------------------
/wwwroot/js/chat.js:
--------------------------------------------------------------------------------
1 | var connection = new signalR.HubConnectionBuilder()
2 | .withUrl("/chat")
3 | .withAutomaticReconnect([0, 3000, 5000, 10000, 15000, 30000])
4 | //.withAutomaticReconnect([0, 2000, 10000, 30000]) yields the default behavior
5 | .build();
6 |
7 | connection.onreconnecting((error) => {
8 | disableUi(true);
9 | const li = document.createElement("li");
10 | li.textContent = `Connection lost due to error "${error}". Reconnecting.`;
11 | document.getElementById("messagesList").appendChild(li);
12 | });
13 |
14 | connection.onreconnected((connectionId) => {
15 | disableUi(false);
16 | const li = document.createElement("li");
17 | li.textContent = `Connection reestablished. Connected.`;
18 | document.getElementById("messagesList").appendChild(li);
19 | });
20 |
21 | function disableUi(isEnabled) {
22 | document.getElementById("leave-group").disabled = isEnabled;
23 | document.getElementById("join-group").disabled = isEnabled;
24 | document.getElementById("groupmsg").disabled = isEnabled;
25 | }
26 |
27 | connection.on("Send", function (message) {
28 | var li = document.createElement("li");
29 | li.textContent = message;
30 | document.getElementById("messagesList").appendChild(li);
31 | });
32 |
33 | document.getElementById("groupmsg").addEventListener("click", async (event) => {
34 | var userName = document.getElementById("user-name").value;
35 | var groupName = document.getElementById("group-name").value;
36 | var groupMsg = document.getElementById("group-message-text").value;
37 | try {
38 | await connection.invoke("SendMessageToGroup", userName, groupName, groupMsg);
39 | }
40 | catch (e) {
41 | console.error(e.toString());
42 | }
43 | event.preventDefault();
44 | });
45 |
46 | document.getElementById("join-group").addEventListener("click", async (event) => {
47 | var userName = document.getElementById("user-name").value;
48 | var groupName = document.getElementById("group-name").value;
49 | try {
50 | await connection.invoke("AddToGroup", userName, groupName);
51 | }
52 | catch (e) {
53 | console.error(e.toString());
54 | }
55 | event.preventDefault();
56 | });
57 |
58 | document.getElementById("leave-group").addEventListener("click", async (event) => {
59 | var userName = document.getElementById("user-name").value;
60 | var groupName = document.getElementById("group-name").value;
61 | try {
62 | await connection.invoke("RemoveFromGroup", userName, groupName);
63 | }
64 | catch (e) {
65 | console.error(e.toString());
66 | }
67 | event.preventDefault();
68 | });
69 |
70 | async function start() {
71 | try {
72 | await connection.start();
73 | console.log("connected");
74 | } catch (err) {
75 | console.log(err);
76 | }
77 | };
78 |
79 | start();
--------------------------------------------------------------------------------
/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
4 | // for details on configuring this project to bundle and minify static web assets.
5 |
6 | // Write your JavaScript code.
7 |
--------------------------------------------------------------------------------