Skip to content

Commit 94e36fd

Browse files
vicancymitchdennyeerhardt
authored
Adding WebPubSub component and provisioning (dotnet#2495)
* Adding webpubsub components and provisioning * Fix build * Add HubName option * Fix tests Address minor PR feedback --------- Co-authored-by: Mitch Denny <[email protected]> Co-authored-by: Eric Erhardt <[email protected]>
1 parent 78b36fe commit 94e36fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1747
-2
lines changed

Aspire.sln

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio Version 17
33
VisualStudioVersion = 17.0.31903.59
44
MinimumVisualStudioVersion = 10.0.40219.1
@@ -335,6 +335,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxylessEndToEnd.ApiServic
335335
EndProject
336336
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxylessEndToEnd.AppHost", "playground\ProxylessEndToEnd\ProxylessEndToEnd.AppHost\ProxylessEndToEnd.AppHost.csproj", "{0244203D-7491-4414-9C88-10BFED9C5B2D}"
337337
EndProject
338+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Azure.Messaging.WebPubSub", "src\Components\Aspire.Azure.Messaging.WebPubSub\Aspire.Azure.Messaging.WebPubSub.csproj", "{7C07AF64-1E53-4640-A7EF-6EE23D2F0BDA}"
339+
EndProject
340+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Azure.Messaging.WebPubSub.Tests", "tests\Aspire.Azure.Messaging.WebPubSub.Tests\Aspire.Azure.Messaging.WebPubSub.Tests.csproj", "{D13F77BA-1FD1-4CEA-AEBB-9A28B033B2D3}"
341+
EndProject
342+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "webpubsub", "webpubsub", "{90A70EFA-F26A-49E0-A375-DB461E4E0E25}"
343+
EndProject
344+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebPubSubAppHost", "playground\webpubsub\WebPubSubAppHost\WebPubSubAppHost.csproj", "{36B782B3-9515-4AF0-BDB1-52062CECF6FD}"
345+
EndProject
346+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebPubSubWeb", "playground\webpubsub\WebPubSubWeb\WebPubSubWeb.csproj", "{B1EB813D-1CAA-406F-9786-756939B7232C}"
347+
EndProject
338348
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Seq", "src\Components\Aspire.Seq\Aspire.Seq.csproj", "{42F560BA-BEB0-4A95-B673-5E50BF3EFB5E}"
339349
EndProject
340350
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Seq.ServiceDefaults", "playground\seq\Seq.ServiceDefaults\Seq.ServiceDefaults.csproj", "{E785C7A8-F8A3-49D6-9600-9D8E10FB5624}"
@@ -475,6 +485,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Milvus.Client", "src
475485
EndProject
476486
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Milvus.Client.Tests", "tests\Aspire.Milvus.Client.Tests\Aspire.Milvus.Client.Tests.csproj", "{9FAE1602-2C69-4D24-8655-A164489441E8}"
477487
EndProject
488+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Azure.WebPubSub", "src\Aspire.Hosting.Azure.WebPubSub\Aspire.Hosting.Azure.WebPubSub.csproj", "{DF00FDA3-D3EC-4E07-B4EC-0EBB57A813A4}"
489+
EndProject
478490
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Hosting.Valkey", "src\Aspire.Hosting.Valkey\Aspire.Hosting.Valkey.csproj", "{5CB63205-24F4-4388-A41B-BAF3BEA59866}"
479491
EndProject
480492
Global
@@ -1011,6 +1023,22 @@ Global
10111023
{0244203D-7491-4414-9C88-10BFED9C5B2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
10121024
{0244203D-7491-4414-9C88-10BFED9C5B2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
10131025
{0244203D-7491-4414-9C88-10BFED9C5B2D}.Release|Any CPU.Build.0 = Release|Any CPU
1026+
{7C07AF64-1E53-4640-A7EF-6EE23D2F0BDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1027+
{7C07AF64-1E53-4640-A7EF-6EE23D2F0BDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
1028+
{7C07AF64-1E53-4640-A7EF-6EE23D2F0BDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
1029+
{7C07AF64-1E53-4640-A7EF-6EE23D2F0BDA}.Release|Any CPU.Build.0 = Release|Any CPU
1030+
{D13F77BA-1FD1-4CEA-AEBB-9A28B033B2D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1031+
{D13F77BA-1FD1-4CEA-AEBB-9A28B033B2D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
1032+
{D13F77BA-1FD1-4CEA-AEBB-9A28B033B2D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
1033+
{D13F77BA-1FD1-4CEA-AEBB-9A28B033B2D3}.Release|Any CPU.Build.0 = Release|Any CPU
1034+
{36B782B3-9515-4AF0-BDB1-52062CECF6FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1035+
{36B782B3-9515-4AF0-BDB1-52062CECF6FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
1036+
{36B782B3-9515-4AF0-BDB1-52062CECF6FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
1037+
{36B782B3-9515-4AF0-BDB1-52062CECF6FD}.Release|Any CPU.Build.0 = Release|Any CPU
1038+
{B1EB813D-1CAA-406F-9786-756939B7232C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1039+
{B1EB813D-1CAA-406F-9786-756939B7232C}.Debug|Any CPU.Build.0 = Debug|Any CPU
1040+
{B1EB813D-1CAA-406F-9786-756939B7232C}.Release|Any CPU.ActiveCfg = Release|Any CPU
1041+
{B1EB813D-1CAA-406F-9786-756939B7232C}.Release|Any CPU.Build.0 = Release|Any CPU
10141042
{42F560BA-BEB0-4A95-B673-5E50BF3EFB5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
10151043
{42F560BA-BEB0-4A95-B673-5E50BF3EFB5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
10161044
{42F560BA-BEB0-4A95-B673-5E50BF3EFB5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -1255,6 +1283,10 @@ Global
12551283
{9FAE1602-2C69-4D24-8655-A164489441E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
12561284
{9FAE1602-2C69-4D24-8655-A164489441E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
12571285
{9FAE1602-2C69-4D24-8655-A164489441E8}.Release|Any CPU.Build.0 = Release|Any CPU
1286+
{DF00FDA3-D3EC-4E07-B4EC-0EBB57A813A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1287+
{DF00FDA3-D3EC-4E07-B4EC-0EBB57A813A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
1288+
{DF00FDA3-D3EC-4E07-B4EC-0EBB57A813A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
1289+
{DF00FDA3-D3EC-4E07-B4EC-0EBB57A813A4}.Release|Any CPU.Build.0 = Release|Any CPU
12581290
EndGlobalSection
12591291
GlobalSection(SolutionProperties) = preSolution
12601292
HideSolutionNode = FALSE
@@ -1413,6 +1445,11 @@ Global
14131445
{9C30FFD6-2262-45E7-B010-24B30E0433C2} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
14141446
{51654CD7-2E05-4664-B2EB-95308A300609} = {9C30FFD6-2262-45E7-B010-24B30E0433C2}
14151447
{0244203D-7491-4414-9C88-10BFED9C5B2D} = {9C30FFD6-2262-45E7-B010-24B30E0433C2}
1448+
{7C07AF64-1E53-4640-A7EF-6EE23D2F0BDA} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
1449+
{D13F77BA-1FD1-4CEA-AEBB-9A28B033B2D3} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
1450+
{90A70EFA-F26A-49E0-A375-DB461E4E0E25} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
1451+
{36B782B3-9515-4AF0-BDB1-52062CECF6FD} = {90A70EFA-F26A-49E0-A375-DB461E4E0E25}
1452+
{B1EB813D-1CAA-406F-9786-756939B7232C} = {90A70EFA-F26A-49E0-A375-DB461E4E0E25}
14161453
{42F560BA-BEB0-4A95-B673-5E50BF3EFB5E} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
14171454
{E785C7A8-F8A3-49D6-9600-9D8E10FB5624} = {78117273-982B-46F2-BA69-402C42294335}
14181455
{78117273-982B-46F2-BA69-402C42294335} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0}
@@ -1483,6 +1520,7 @@ Global
14831520
{1A3A4865-69F2-4251-9A95-2D57C6A46870} = {B80354C7-BE58-43F6-8928-9F3A74AB7F47}
14841521
{1BFE3C02-3B81-4596-99A2-4DCDD9129C9A} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2}
14851522
{9FAE1602-2C69-4D24-8655-A164489441E8} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
1523+
{DF00FDA3-D3EC-4E07-B4EC-0EBB57A813A4} = {77CFE74A-32EE-400C-8930-5025E8555256}
14861524
EndGlobalSection
14871525
GlobalSection(ExtensibilityGlobals) = postSolution
14881526
SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C}

Directory.Packages.props

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<PackageVersion Include="Azure.Messaging.EventHubs.Processor" Version="5.11.3" />
2626
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.17.5" />
2727
<PackageVersion Include="Azure.Search.Documents" Version="11.5.1" />
28+
<PackageVersion Include="Azure.Messaging.WebPubSub" Version="1.3.0" />
2829
<PackageVersion Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
2930
<PackageVersion Include="Azure.Storage.Blobs" Version="12.20.0" />
3031
<PackageVersion Include="Azure.Storage.Queues" Version="12.18.0" />
@@ -49,6 +50,7 @@
4950
<PackageVersion Include="Azure.Provisioning.SignalR" Version="0.2.0" />
5051
<PackageVersion Include="Azure.Provisioning.Sql" Version="0.1.0" />
5152
<PackageVersion Include="Azure.Provisioning.Storage" Version="0.2.0" />
53+
<PackageVersion Include="Azure.Provisioning.WebPubSub" Version="0.1.0-beta.1" />
5254
<!-- ASP.NET Core dependencies -->
5355
<PackageVersion Include="Microsoft.AspNetCore.Authentication.Certificate" Version="$(MicrosoftAspNetCoreAuthenticationCertificatePackageVersion)" />
5456
<PackageVersion Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="$(MicrosoftAspNetCoreAuthenticationOpenIdConnectPackageVersion)" />

playground/bicep/BicepSample.AppHost/BicepSample.AppHost.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.Storage\Aspire.Hosting.Azure.Storage.csproj" IsAspireProjectResource="false" />
2525
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.ServiceBus\Aspire.Hosting.Azure.ServiceBus.csproj" IsAspireProjectResource="false" />
2626
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.SignalR\Aspire.Hosting.Azure.SignalR.csproj" IsAspireProjectResource="false" />
27+
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.WebPubSub\Aspire.Hosting.Azure.WebPubSub.csproj" IsAspireProjectResource="false" />
2728
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.AppConfiguration\Aspire.Hosting.Azure.AppConfiguration.csproj" IsAspireProjectResource="false" />
2829
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.ApplicationInsights\Aspire.Hosting.Azure.ApplicationInsights.csproj" IsAspireProjectResource="false" />
2930
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.Sql\Aspire.Hosting.Azure.Sql.csproj" IsAspireProjectResource="false" />

playground/bicep/BicepSample.AppHost/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
.AddTopic("topic1", ["subscription1", "subscription2"])
6060
.AddTopic("topic2", ["subscription1"]);
6161
var signalr = builder.AddAzureSignalR("signalr");
62-
62+
var webpubsub = builder.AddAzureWebPubSub("wps");
6363
builder.AddProject<Projects.BicepSample_ApiService>("api")
6464
.WithExternalHttpEndpoints()
6565
.WithReference(sqlServer)
@@ -74,6 +74,7 @@
7474
.WithReference(redis)
7575
.WithReference(serviceBus)
7676
.WithReference(signalr)
77+
.WithReference(webpubsub)
7778
.WithEnvironment("bicepValue_test", bicep1.GetOutput("test"))
7879
.WithEnvironment("bicepValue0", bicep1.GetOutput("val0"))
7980
.WithEnvironment("bicepValue1", bicep1.GetOutput("val1"));

playground/bicep/BicepSample.AppHost/aspire-manifest.json

+10
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@
165165
"principalType": ""
166166
}
167167
},
168+
"wps": {
169+
"type": "azure.bicep.v0",
170+
"connectionString": "{wps.outputs.endpoint}",
171+
"path": "wps.module.bicep",
172+
"params": {
173+
"principalId": "",
174+
"principalType": ""
175+
}
176+
},
168177
"api": {
169178
"type": "project.v0",
170179
"path": "../BicepSample.ApiService/BicepSample.ApiService.csproj",
@@ -185,6 +194,7 @@
185194
"ConnectionStrings__redis": "{redis.connectionString}",
186195
"ConnectionStrings__sb": "{sb.connectionString}",
187196
"ConnectionStrings__signalr": "{signalr.connectionString}",
197+
"ConnectionStrings__wps": "{wps.connectionString}",
188198
"bicepValue_test": "{test.outputs.test}",
189199
"bicepValue0": "{test.outputs.val0}",
190200
"bicepValue1": "{test.outputs.val1}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
targetScope = 'resourceGroup'
2+
3+
@description('')
4+
param location string = resourceGroup().location
5+
6+
@description('')
7+
param sku string = 'Free_F1'
8+
9+
@description('')
10+
param capacity int = 1
11+
12+
@description('')
13+
param principalId string
14+
15+
@description('')
16+
param principalType string
17+
18+
19+
resource webPubSubService_e7AgYwyOW 'Microsoft.SignalRService/webPubSub@2021-10-01' = {
20+
name: toLower(take('wps${uniqueString(resourceGroup().id)}', 24))
21+
location: location
22+
tags: {
23+
'aspire-resource-name': 'wps'
24+
}
25+
sku: {
26+
name: sku
27+
capacity: capacity
28+
}
29+
properties: {
30+
}
31+
}
32+
33+
resource roleAssignment_TqCIYDDW7 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
34+
scope: webPubSubService_e7AgYwyOW
35+
name: guid(webPubSubService_e7AgYwyOW.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '12cf5a90-567b-43ae-8102-96cf46c7d9b4'))
36+
properties: {
37+
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '12cf5a90-567b-43ae-8102-96cf46c7d9b4')
38+
principalId: principalId
39+
principalType: principalType
40+
}
41+
}
42+
43+
output endpoint string = 'https://${webPubSubService_e7AgYwyOW.properties.hostName}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
3+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
4+
5+
<!-- NOTE: This line is only required because we are using P2P references, not NuGet. It will not exist in real apps. -->
6+
<Import Project="../../../src/Aspire.Hosting.AppHost/build/Aspire.Hosting.AppHost.props" />
7+
8+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project>
2+
3+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />
4+
5+
<!-- NOTE: These lines are only required because we are using P2P references, not NuGet. They will not exist in real apps. -->
6+
<Import Project="..\..\..\src\Aspire.Hosting.AppHost\build\Aspire.Hosting.AppHost.targets" />
7+
<Import Project="..\..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" />
8+
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
var builder = DistributedApplication.CreateBuilder(args);
3+
4+
builder.AddAzureProvisioning();
5+
6+
var wps = builder.AddAzureWebPubSub("wps1");
7+
8+
builder.AddProject<Projects.WebPubSubWeb>("webfrontend")
9+
.WithExternalHttpEndpoints()
10+
.WithReference(wps);
11+
12+
builder.Build().Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost:15016",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"DOTNET_ENVIRONMENT": "Development",
12+
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16099",
13+
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"
14+
}
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Aspire AppHost sample for Azure Web PubSub
2+
3+
## Usage
4+
5+
Update [appsettings.json](./appsettings.json] to use your `SubscriptionId` and `Location` to provision the Azure Web PubSub resource.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsAspireHost>true</IsAspireHost>
9+
<UserSecretsId>b51e0e93-326b-4371-920f-d41742849af2</UserSecretsId>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Compile Include="$(SharedDir)KnownResourceNames.cs" Link="KnownResourceNames.cs" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\..\src\Aspire.Dashboard\Aspire.Dashboard.csproj" />
17+
<ProjectReference Include="..\..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" IsAspireProjectResource="false" />
18+
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure\Aspire.Hosting.Azure.csproj" IsAspireProjectResource="false" />
19+
<ProjectReference Include="..\..\..\src\Aspire.Hosting.Azure.WebPubSub\Aspire.Hosting.Azure.WebPubSub.csproj" IsAspireProjectResource="false" />
20+
21+
<ProjectReference Include="..\WebPubSubWeb\WebPubSubWeb.csproj" />
22+
</ItemGroup>
23+
24+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
},
9+
"Azure": {
10+
"SubscriptionId": "",
11+
"Location": ""
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
@page
2+
<div class="container">
3+
<div class="row p-1">
4+
<div class="col-1">Message</div>
5+
<div class="col-5"><input type="text" class="w-100" id="messageInput" /></div>
6+
</div>
7+
<div class="row p-1">
8+
<div class="col-6 text-end">
9+
<input type="button" id="sendButton" value="Send Message" />
10+
</div>
11+
</div>
12+
<div class="row p-1">
13+
<div class="col-6">
14+
<hr />
15+
</div>
16+
</div>
17+
<div class="row p-1">
18+
<div class="col-6">
19+
<ul id="messagesList"></ul>
20+
</div>
21+
</div>
22+
</div>
23+
<script>
24+
function addMessage(content) {
25+
var li = document.createElement("li");
26+
document.getElementById("messagesList").appendChild(li);
27+
li.textContent = content;
28+
}
29+
async function connect(hubName) {
30+
let res = await fetch('/negotiate/' + hubName)
31+
let data = await res.json();
32+
return new WebSocket(data.url, 'json.webpubsub.azure.v1');
33+
}
34+
(async function () {
35+
//Disable the send button until connection is established.
36+
document.getElementById("sendButton").disabled = true;
37+
// chat connection for the clients to talk to each other
38+
let chatConnection = await connect("chat");
39+
chatConnection.onopen = () => {
40+
document.getElementById("sendButton").disabled = false;
41+
chatConnection.send(JSON.stringify({
42+
type: 'joinGroup',
43+
group: 'group1'
44+
}));
45+
addMessage("connected to chat hub");
46+
};
47+
let output = document.querySelector('#output');
48+
chatConnection.onmessage = event => {
49+
let message = JSON.parse(event.data);
50+
if (message.type === 'message' && message.group === 'group1') {
51+
addMessage(message.data);
52+
}
53+
};
54+
document.getElementById("sendButton").addEventListener("click", function (event) {
55+
var message = document.getElementById("messageInput").value;
56+
chatConnection.send(JSON.stringify({
57+
type: 'sendToGroup',
58+
group: 'group1',
59+
data: message,
60+
dataType: "text"
61+
}));
62+
event.preventDefault();
63+
});
64+
// notification hub to receive data from the server
65+
let notificationConnection = await connect("notification");
66+
notificationConnection.onopen = () => {
67+
addMessage("connected to notification hub");
68+
};
69+
notificationConnection.onmessage = event => {
70+
let message = JSON.parse(event.data);
71+
if (message.type === 'message') {
72+
addMessage("Notification:" + message.data);
73+
}
74+
};
75+
})();
76+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.AspNetCore.Mvc.RazorPages;
2+
3+
namespace WebPubSubWeb.Pages;
4+
5+
public class IndexModel : PageModel
6+
{
7+
private readonly ILogger<IndexModel> _logger;
8+
9+
public IndexModel(ILogger<IndexModel> logger)
10+
{
11+
_logger = logger;
12+
}
13+
14+
public void OnGet()
15+
{
16+
17+
}
18+
}

0 commit comments

Comments
 (0)