From af6060d7ab1b456eb75861128591580311cf3a85 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Thu, 31 Aug 2017 17:36:45 +0200 Subject: [PATCH] [TRY] --- .../Musical/PodcastController.cs | 12 +---- Yavsc/Controllers/HomeController.cs | 4 ++ Yavsc/Startup/Startup.WebSockets.cs | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/Yavsc/ApiControllers/Musical/PodcastController.cs b/Yavsc/ApiControllers/Musical/PodcastController.cs index 8bb5a5db..7032aa02 100644 --- a/Yavsc/ApiControllers/Musical/PodcastController.cs +++ b/Yavsc/ApiControllers/Musical/PodcastController.cs @@ -1,22 +1,14 @@ using System; using System.Collections.Concurrent; using System.Net.WebSockets; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Mvc; +using Microsoft.Extensions.Logging; namespace Yavsc.ApiControllers { public class PodcastController : Controller { - public ArraySegment CurrentOutput ; - public bool IsEnd; - - ConcurrentBag Listeners = new ConcurrentBag(); - - public async Task Connect() - { - var socket = await this.HttpContext.WebSockets.AcceptWebSocketAsync(); - - } } } \ No newline at end of file diff --git a/Yavsc/Controllers/HomeController.cs b/Yavsc/Controllers/HomeController.cs index 42b8ff65..5b839ed5 100644 --- a/Yavsc/Controllers/HomeController.cs +++ b/Yavsc/Controllers/HomeController.cs @@ -124,6 +124,10 @@ namespace Yavsc.Controllers return View(); } + public IActionResult VideoChat() + { + return View(); + } } } diff --git a/Yavsc/Startup/Startup.WebSockets.cs b/Yavsc/Startup/Startup.WebSockets.cs index 563131fd..1d1a1de8 100644 --- a/Yavsc/Startup/Startup.WebSockets.cs +++ b/Yavsc/Startup/Startup.WebSockets.cs @@ -1,15 +1,66 @@ +using System; +using System.Collections.Concurrent; +using System.Net.WebSockets; +using System.Threading; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; +using Yavsc.Extensions; namespace Yavsc { public partial class Startup { + public static ConcurrentBag Listeners = new ConcurrentBag(); public void ConfigureWebSocketsApp(IApplicationBuilder app, SiteSettings siteSettings, IHostingEnvironment env) { app.UseWebSockets(); app.UseSignalR("/api/signalr"); + app.UseWhen(context => context.Request.Path.StartsWithSegments("/ws"), + branch => + { + branch.Use( + async (http, next) => + { + if (http.WebSockets.IsWebSocketRequest) + { + WebSocket webSocket = null; + if (!Listeners.TryPeek(out webSocket)) + { + webSocket = await http.WebSockets.AcceptWebSocketAsync(); + Listeners.Add(webSocket); + } + using (webSocket) + { + if (webSocket != null && webSocket.State == WebSocketState.Open) + { + // TODO: Handle the socket here. + byte[] buffer = new byte[1024]; + WebSocketReceiveResult received = await webSocket.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); + while (received.MessageType != WebSocketMessageType.Close) + { + Console.WriteLine($"Echoing {received.Count} bytes received in a {received.MessageType} message; Fin={received.EndOfMessage}"); + // Echo anything we receive + await webSocket.SendAsync(new ArraySegment(buffer, 0, received.Count), received.MessageType, received.EndOfMessage, CancellationToken.None); + + received = await webSocket.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); + } + await webSocket.CloseAsync(received.CloseStatus.Value, received.CloseStatusDescription, CancellationToken.None); + + } + + } + } + else + { + // Nothing to do here, pass downstream. + await next(); + } + } + ); + } + ); + /* var _sockets = new ConcurrentBag();