microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.8

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
4using System.Net.WebSockets;
5using System.Reflection;
6
7using Microsoft.AspNetCore.Connections;
8using Microsoft.AspNetCore.Http;
9using Microsoft.AspNetCore.Mvc;
10using Microsoft.Extensions.FileProviders;
11using Microsoft.Extensions.Hosting;
12using Microsoft.Teams.Plugins.AspNetCore.DevTools.Events;
13using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions;
14
15namespace Microsoft.Teams.Plugins.AspNetCore.DevTools.Controllers;
16
17[ApiController]
18public 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}