Хаб + веб-клиент
This commit is contained in:
parent
675b1aa2ab
commit
420edfc4f4
12
ExampleSignalR/Hubs/ChatHub.cs
Normal file
12
ExampleSignalR/Hubs/ChatHub.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
28
ExampleSignalR/Pages/Index.cshtml
Normal file
28
ExampleSignalR/Pages/Index.cshtml
Normal file
@ -0,0 +1,28 @@
|
||||
@page
|
||||
<div class="container">
|
||||
<div class="row p-1">
|
||||
<div class="col-1">User</div>
|
||||
<div class="col-5"><input type="text" id="userInput" /></div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-1">Message</div>
|
||||
<div class="col-5"><input type="text" class="w-100" id="messageInput" /></div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-6 text-end">
|
||||
<input type="button" id="sendButton" value="Send Message" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-6">
|
||||
<hr />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-6">
|
||||
<ul id="messagesList"></ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="~/js/signalr/dist/browser/signalr.js"></script>
|
||||
<script src="~/js/chat.js"></script>
|
36
ExampleSignalR/Program.cs
Normal file
36
ExampleSignalR/Program.cs
Normal file
@ -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>("/chatHub",
|
||||
options => {
|
||||
options.ApplicationMaxBufferSize = 128;
|
||||
options.TransportMaxBufferSize = 128;
|
||||
options.LongPolling.PollTimeout = TimeSpan.FromMinutes(1);
|
||||
options.Transports = HttpTransportType.LongPolling | HttpTransportType.WebSockets;
|
||||
});
|
||||
|
||||
app.Run();
|
28
ExampleSignalR/wwwroot/Pages/Index.cshtml
Normal file
28
ExampleSignalR/wwwroot/Pages/Index.cshtml
Normal file
@ -0,0 +1,28 @@
|
||||
@page
|
||||
<div class="container">
|
||||
<div class="row p-1">
|
||||
<div class="col-1">User</div>
|
||||
<div class="col-5"><input type="text" id="userInput" /></div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-1">Message</div>
|
||||
<div class="col-5"><input type="text" class="w-100" id="messageInput" /></div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-6 text-end">
|
||||
<input type="button" id="sendButton" value="Send Message" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-6">
|
||||
<hr />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="col-6">
|
||||
<ul id="messagesList"></ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="~/js/signalr/dist/browser/signalr.js"></script>
|
||||
<script src="~/js/chat.js"></script>
|
32
ExampleSignalR/wwwroot/js/chat.js
Normal file
32
ExampleSignalR/wwwroot/js/chat.js
Normal file
@ -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();
|
||||
});
|
Loading…
Reference in New Issue
Block a user