diff --git a/ExampleSignalR/Hubs/ChatHub.cs b/ExampleSignalR/Hubs/ChatHub.cs new file mode 100644 index 0000000..74b096a --- /dev/null +++ b/ExampleSignalR/Hubs/ChatHub.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.SignalR; + +namespace ExampleSignalR.Hubs +{ + public class ChatHub : Hub + { + public async Task SendMessage(string user, string message) + { + await Clients.All.SendAsync("ReceiveMessage", user, message); + } + } +} diff --git a/ExampleSignalR/Pages/Index.cshtml b/ExampleSignalR/Pages/Index.cshtml new file mode 100644 index 0000000..f476ba1 --- /dev/null +++ b/ExampleSignalR/Pages/Index.cshtml @@ -0,0 +1,28 @@ +@page +
+
+
User
+
+
+
+
Message
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
    +
    +
    +
    + + \ No newline at end of file diff --git a/ExampleSignalR/Program.cs b/ExampleSignalR/Program.cs new file mode 100644 index 0000000..64830f6 --- /dev/null +++ b/ExampleSignalR/Program.cs @@ -0,0 +1,36 @@ +using ExampleSignalR.Hubs; +using Microsoft.AspNetCore.Http.Connections; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddRazorPages(); +builder.Services.AddSignalR(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (!app.Environment.IsDevelopment()) +{ + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); +} + +app.UseHttpsRedirection(); +app.UseStaticFiles(); + +app.UseRouting(); + +app.UseAuthorization(); + +app.MapRazorPages(); +app.MapHub("/chatHub", + options => { + options.ApplicationMaxBufferSize = 128; + options.TransportMaxBufferSize = 128; + options.LongPolling.PollTimeout = TimeSpan.FromMinutes(1); + options.Transports = HttpTransportType.LongPolling | HttpTransportType.WebSockets; + }); + +app.Run(); diff --git a/ExampleSignalR/wwwroot/Pages/Index.cshtml b/ExampleSignalR/wwwroot/Pages/Index.cshtml new file mode 100644 index 0000000..f476ba1 --- /dev/null +++ b/ExampleSignalR/wwwroot/Pages/Index.cshtml @@ -0,0 +1,28 @@ +@page +
    +
    +
    User
    +
    +
    +
    +
    Message
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    +
    +
    +
    +
      +
      +
      +
      + + \ No newline at end of file diff --git a/ExampleSignalR/wwwroot/js/chat.js b/ExampleSignalR/wwwroot/js/chat.js new file mode 100644 index 0000000..e83611d --- /dev/null +++ b/ExampleSignalR/wwwroot/js/chat.js @@ -0,0 +1,32 @@ +"use strict"; + +var connection = new signalR.HubConnectionBuilder() + .withUrl("/chatHub") + .build(); + +//Disable the send button until connection is established. +document.getElementById("sendButton").disabled = true; + +connection.on("ReceiveMessage", function (user, message) { + var li = document.createElement("li"); + document.getElementById("messagesList").appendChild(li); + // We can assign user-supplied strings to an element's textContent because it + // is not interpreted as markup. If you're assigning in any other way, you + // should be aware of possible script injection concerns. + li.textContent = `${user} says ${message}`; +}); + +connection.start().then(function () { + document.getElementById("sendButton").disabled = false; +}).catch(function (err) { + return console.error(err.toString()); +}); + +document.getElementById("sendButton").addEventListener("click", function (event) { + var user = document.getElementById("userInput").value; + var message = document.getElementById("messageInput").value; + connection.invoke("SendMessage", user, message).catch(function (err) { + return console.error(err.toString()); + }); + event.preventDefault(); +}); \ No newline at end of file