microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.DevTools/Controllers/DevToolsController.cs
86lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Net.WebSockets; |
| 5 | using System.Reflection; |
| 6 | |
| 7 | using Microsoft.AspNetCore.Connections; |
| 8 | using Microsoft.AspNetCore.Http; |
| 9 | using Microsoft.AspNetCore.Mvc; |
| 10 | using Microsoft.Extensions.FileProviders; |
| 11 | using Microsoft.Extensions.Hosting; |
| 12 | using Microsoft.Teams.Plugins.AspNetCore.DevTools.Events; |
| 13 | using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions; |
| 14 | |
| 15 | namespace Microsoft.Teams.Plugins.AspNetCore.DevTools.Controllers; |
| 16 | |
| 17 | [ApiController] |
| 18 | public class DevToolsController : ControllerBase |
| 19 | { |
| 20 | private readonly DevToolsPlugin _plugin; |
| 21 | private readonly IFileProvider _files; |
| 22 | private readonly IHostApplicationLifetime _lifetime; |
| 23 | |
| 24 | public DevToolsController(DevToolsPlugin plugin, IHostApplicationLifetime lifetime) |
| 25 | { |
| 26 | _plugin = plugin; |
| 27 | _files = new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly(), "web"); |
| 28 | _lifetime = lifetime; |
| 29 | } |
| 30 | |
| 31 | [HttpGet("/devtools")] |
| 32 | [HttpGet("/devtools/{*path}")] |
| 33 | public IResult Get(string? path) |
| 34 | { |
| 35 | var file = _files.GetFileInfo(path ?? "index.html"); |
| 36 | |
| 37 | if (!file.Exists) |
| 38 | { |
| 39 | return Get("index.html"); |
| 40 | } |
| 41 | |
| 42 | return Results.File(file.CreateReadStream(), contentType: "text/html"); |
| 43 | } |
| 44 | |
| 45 | [HttpGet("/devtools/sockets")] |
| 46 | public async Task GetSocket() |
| 47 | { |
| 48 | if (!HttpContext.WebSockets.IsWebSocketRequest) |
| 49 | { |
| 50 | HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | using var socket = await HttpContext.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false); |
| 55 | var id = Guid.NewGuid().ToString(); |
| 56 | var buffer = new byte[1024]; |
| 57 | |
| 58 | _plugin.Sockets.Add(id, socket); |
| 59 | await _plugin.Sockets.Emit(id, new MetaDataEvent(_plugin.MetaData), _lifetime.ApplicationStopping).ConfigureAwait(false); |
| 60 | |
| 61 | try |
| 62 | { |
| 63 | while (socket.State.HasFlag(WebSocketState.Open)) |
| 64 | { |
| 65 | await socket.ReceiveAsync(buffer, _lifetime.ApplicationStopping).ConfigureAwait(false); |
| 66 | } |
| 67 | } |
| 68 | catch (ConnectionAbortedException) |
| 69 | { |
| 70 | |
| 71 | } |
| 72 | catch (OperationCanceledException) |
| 73 | { |
| 74 | |
| 75 | } |
| 76 | finally |
| 77 | { |
| 78 | if (socket.IsCloseable()) |
| 79 | { |
| 80 | await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, _lifetime.ApplicationStopping).ConfigureAwait(false); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | _plugin.Sockets.Remove(id); |
| 85 | } |
| 86 | } |