Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aspnetcore-webapi/RestVsWebSocket/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Net.WebSockets;
using System.Net;
using System.Text;

namespace RestVsWebSocket.Controllers;
Expand Down Expand Up @@ -35,32 +34,15 @@ public IActionResult AddTask([FromBody] string task)
[HttpGet]
public async Task Get()
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();

while (true)
{
var message = "The time is: " + DateTime.Now.ToString("HH:mm:ss");
var bytes = Encoding.UTF8.GetBytes(message);
var arraySegment = new ArraySegment<byte>(bytes, 0, bytes.Length);
if (ws.State == WebSocketState.Open)
{
await ws.SendAsync(arraySegment,
WebSocketMessageType.Text,
true,
CancellationToken.None);
}
else if (ws.State == WebSocketState.Closed || ws.State == WebSocketState.Aborted)
{
break;
}
Thread.Sleep(1000);
}
}
else
while (ws.State == WebSocketState.Open)
{
HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
var message = $"The time is: {DateTime.Now:HH:mm:ss}";
var bytes = Encoding.UTF8.GetBytes(message);

await ws.SendAsync(bytes, WebSocketMessageType.Text, true, CancellationToken.None);
await Task.Delay(1000);
}
}
}
22 changes: 4 additions & 18 deletions aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Program.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOpenApi();

builder.WebHost.UseUrls("http://localhost:5289");
builder.WebHost.UseUrls("http://localhost:5289"); // Client/Program.cs hardcodes this URL

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();
app.MapOpenApi();
app.UseWebSockets();
app.MapControllers();

app.Run();
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion aspnetcore-webapi/RestVsWebSocket/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
Loading