Instantly share code, notes, and snippets.

View message.js
"use strict";
var connection = new signalR.HubConnectionBuilder()
.withUrl("/messages")
.build();
connection.on("ReceiveMessage", function(message) {
var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
var div = document.createElement("div");
div.innerHTML = msg + "<hr/>";
View index.cshtml
@page
<div>
<input type="button" id="joinGroup" value="Join Private Group" />
</div>
<textarea name="message" id="message"></textarea>
<select id="group">
<option value="All">Everyone</option>
<option value="Myself">Myself</option>
<option value="PrivateGroup">Private Group</option>
View MessageHub.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Practical.AspNetCore.SignalR
{
public class MessageHub : Hub
{
public Task SendMessageToAll(string message)
View MessageHub.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Practical.AspNetCore.SignalR
{
public class MessageHub : Hub
{
public Task SendMessageToAll(string message)
View MessageHub.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Practical.AspNetCore.SignalR
{
public class MessageHub : Hub
{
public Task SendMessageToAll(string message)
View MessageHub.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Practical.AspNetCore.SignalR
{
public class MessageHub : Hub
{
public Task SendMessageToAll(string message)
View messages.js
"use strict";
var connection = new signalR.HubConnectionBuilder()
.withUrl("/messages")
.build();
connection.on("ReceiveMessage", function(message) {
var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
var div = document.createElement("div");
div.innerHTML = msg + "<hr/>";
View index.cshtml
@page
<textarea name="message" id="message"></textarea>
<input type="button" id="sendButton" value="Send Message"/>
<div id="messages"></div>
<script src="~/lib/signalr.js"></script>
<script src="~/js/message.js"></script>
View Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
View MessageHub.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Practical.AspNetCore.SignalR
{
public class MessageHub : Hub
{
public Task SendMessageToAll(string message)