From 420edfc4f45897e279f1740dd00d289a2fb1ac1b Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Tue, 12 Nov 2024 13:53:51 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A5=D0=B0=D0=B1=20+=20=D0=B2=D0=B5=D0=B1-?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExampleSignalR/Hubs/ChatHub.cs | 12 ++++++++ ExampleSignalR/Pages/Index.cshtml | 28 ++++++++++++++++++ ExampleSignalR/Program.cs | 36 +++++++++++++++++++++++ ExampleSignalR/wwwroot/Pages/Index.cshtml | 28 ++++++++++++++++++ ExampleSignalR/wwwroot/js/chat.js | 32 ++++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 ExampleSignalR/Hubs/ChatHub.cs create mode 100644 ExampleSignalR/Pages/Index.cshtml create mode 100644 ExampleSignalR/Program.cs create mode 100644 ExampleSignalR/wwwroot/Pages/Index.cshtml create mode 100644 ExampleSignalR/wwwroot/js/chat.js 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