diff --git a/openvidu-basic-dotnet/.gitignore b/openvidu-basic-dotnet/.gitignore new file mode 100644 index 00000000..06262728 --- /dev/null +++ b/openvidu-basic-dotnet/.gitignore @@ -0,0 +1,37 @@ +*.swp +*.*~ +project.lock.json +.DS_Store +*.pyc +nupkg/ + +# Visual Studio Code +.vscode + +# Rider +.idea + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +msbuild.log +msbuild.err +msbuild.wrn + +# Visual Studio 2015 +.vs/ \ No newline at end of file diff --git a/openvidu-basic-dotnet/Program.cs b/openvidu-basic-dotnet/Program.cs new file mode 100644 index 00000000..44de0cb6 --- /dev/null +++ b/openvidu-basic-dotnet/Program.cs @@ -0,0 +1,65 @@ +using System.Text; +using System.Net; +using System.Net.Http.Headers; +using Microsoft.AspNetCore.Mvc; + +var builder = WebApplication.CreateBuilder(args); + +var MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; + +// Enable CORS support +builder.Services.AddCors(options => +{ + options.AddPolicy(name: MyAllowSpecificOrigins, + builder => + { + builder.WithOrigins("*").AllowAnyHeader(); + }); +}); + +var app = builder.Build(); +app.UseHttpsRedirection(); +app.UseCors(MyAllowSpecificOrigins); + +IConfiguration config = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .AddEnvironmentVariables().Build(); + +var OPENVIDU_URL = config.GetValue("OPENVIDU_URL"); +var OPENVIDU_SECRET = config.GetValue("OPENVIDU_SECRET"); + +// Allow for insecure certificate in OpenVidu deployment +var handler = new HttpClientHandler +{ + ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator +}; +HttpClient client = new HttpClient(handler); +client.BaseAddress = new System.Uri(OPENVIDU_URL); + +// Set OpenVidu deployment secret +var basicAuth = Convert.ToBase64String(System.Text.ASCIIEncoding.UTF8.GetBytes($"OPENVIDUAPP:{OPENVIDU_SECRET}")); +client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth); + +app.MapPost("/sessions", async (HttpContext context) => +{ + HttpContent content = new StringContent(context.Request.Body.ToString(), Encoding.UTF8, "application/json"); + HttpResponseMessage response = await client.PostAsJsonAsync("openvidu/api/sessions", content); + if (response.StatusCode == HttpStatusCode.Conflict) { + var json = await context.Request.ReadFromJsonAsync>(); + return json["customSessionId"]; + } + response.EnsureSuccessStatusCode(); + var responseBody = await response.Content.ReadFromJsonAsync>(); + return responseBody["sessionId"]; +}); + +app.MapPost("/sessions/{sessionId}/connections", async ([FromRoute] string sessionId, [FromBody] string json) => +{ + HttpContent content = new StringContent(json, Encoding.UTF8, "application/json"); + HttpResponseMessage response = await client.PostAsync("openvidu/api/sessions/" + sessionId + "/connections", content); + response.EnsureSuccessStatusCode(); + string responseBody = await response.Content.ReadAsStringAsync(); +}); + +app.Run(); \ No newline at end of file diff --git a/openvidu-basic-dotnet/Properties/launchSettings.json b/openvidu-basic-dotnet/Properties/launchSettings.json new file mode 100644 index 00000000..f95732d7 --- /dev/null +++ b/openvidu-basic-dotnet/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "https://localhost:5000", + "sslPort": 5000 + } + }, + "profiles": { + "openvidu_basic_dotnet": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/openvidu-basic-dotnet/README.md b/openvidu-basic-dotnet/README.md new file mode 100644 index 00000000..cfa00dd4 --- /dev/null +++ b/openvidu-basic-dotnet/README.md @@ -0,0 +1,13 @@ +# openvidu-basic-dotnet + +This is a minimal OpenVidu server application sample built for .NET. Visit [Application server](https://docs.openvidu.io/en/stable/application-server/) documentation for further context. + +Prerequisites: + +- [.NET 6.0](https://dotnet.microsoft.com/en-us/download) + +Run the application: + +``` +dotnet run +``` \ No newline at end of file diff --git a/openvidu-basic-dotnet/appsettings.Development.json b/openvidu-basic-dotnet/appsettings.Development.json new file mode 100644 index 00000000..ff66ba6b --- /dev/null +++ b/openvidu-basic-dotnet/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/openvidu-basic-dotnet/appsettings.json b/openvidu-basic-dotnet/appsettings.json new file mode 100644 index 00000000..5425520d --- /dev/null +++ b/openvidu-basic-dotnet/appsettings.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "OPENVIDU_URL": "https://localhost:4443/", + "OPENVIDU_SECRET": "MY_SECRET" +} diff --git a/openvidu-basic-dotnet/openvidu-basic-dotnet.csproj b/openvidu-basic-dotnet/openvidu-basic-dotnet.csproj new file mode 100644 index 00000000..941b638b --- /dev/null +++ b/openvidu-basic-dotnet/openvidu-basic-dotnet.csproj @@ -0,0 +1,10 @@ + + + + net6.0 + enable + enable + openvidu_basic_dotnet + + + \ No newline at end of file