mirror of
https://github.com/yawaflua/WebSockets.git
synced 2025-12-09 20:09:32 +02:00
Create project files
This commit is contained in:
17
Examples/ChatController.cs
Normal file
17
Examples/ChatController.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using yawaflua.WebSockets.Attributes;
|
||||
using yawaflua.WebSockets.Models.Abstracts;
|
||||
using WebSocket = yawaflua.WebSockets.Core.WebSocket;
|
||||
|
||||
namespace Examples;
|
||||
|
||||
[WebSocket("/chat")]
|
||||
public class ChatController : WebSocketController
|
||||
{
|
||||
|
||||
public override async Task OnMessageAsync(
|
||||
WebSocket webSocket,
|
||||
HttpContext httpContext)
|
||||
{
|
||||
await WebSocketManager.Broadcast(k => k.Path == "/chat", $"{webSocket.Client.Id}: {webSocket.Message}");
|
||||
}
|
||||
}
|
||||
21
Examples/Dockerfile
Normal file
21
Examples/Dockerfile
Normal file
@@ -0,0 +1,21 @@
|
||||
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["Examples/Examples.csproj", "Examples/"]
|
||||
RUN dotnet restore "Examples/Examples.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/Examples"
|
||||
RUN dotnet build "Examples.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "Examples.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "Examples.dll"]
|
||||
22
Examples/Examples.csproj
Normal file
22
Examples/Examples.csproj
Normal file
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\yawaflua.WebSockets\yawaflua.WebSockets.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
42
Examples/Program.cs
Normal file
42
Examples/Program.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using yawaflua.WebSockets;
|
||||
|
||||
namespace Examples;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
await Host.CreateDefaultBuilder(args)
|
||||
.ConfigureLogging(k => k.AddConsole().AddDebug())
|
||||
.ConfigureWebHost(k =>
|
||||
{
|
||||
k.UseKestrel(l => l.ListenAnyIP(80));
|
||||
k.UseStartup<Startup>();
|
||||
})
|
||||
.RunConsoleAsync();
|
||||
}
|
||||
}
|
||||
|
||||
internal class Startup
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.SettingUpWebSockets();
|
||||
services.AddRouting();
|
||||
services.AddHttpLogging();
|
||||
services.AddSingleton<TestWebSocketServer>();
|
||||
services.AddSingleton<ChatController>();
|
||||
services.AddScoped<IConfiguration>(k => new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json", true)
|
||||
.Build());
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
app.ConnectWebSockets();
|
||||
app.UseRouting();
|
||||
app.UseHttpLogging();
|
||||
app.UseWelcomePage();
|
||||
|
||||
}
|
||||
}
|
||||
18
Examples/TestWebSocketServer.cs
Normal file
18
Examples/TestWebSocketServer.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using yawaflua.WebSockets.Attributes;
|
||||
using yawaflua.WebSockets.Core;
|
||||
using yawaflua.WebSockets.Models.Abstracts;
|
||||
|
||||
namespace Examples;
|
||||
|
||||
[WebSocket("/test")]
|
||||
public class TestWebSocketServer : WebSocketController
|
||||
{
|
||||
|
||||
[WebSocket("/sub-test")]
|
||||
public override async Task OnMessageAsync(WebSocket webSocket, HttpContext httpContext)
|
||||
{
|
||||
await webSocket.SendAsync("Test! Now on it endpoint: " + WebSocketManager.GetAllClients().Count(k => k.Path == webSocket.Client.Path));
|
||||
}
|
||||
}
|
||||
1
Examples/appsettings.json
Normal file
1
Examples/appsettings.json
Normal file
@@ -0,0 +1 @@
|
||||
{}
|
||||
Reference in New Issue
Block a user