main
Paul Schneider 8 years ago
parent 87d4043ec1
commit af6060d7ab
3 changed files with 57 additions and 10 deletions

@ -1,22 +1,14 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging;
namespace Yavsc.ApiControllers namespace Yavsc.ApiControllers
{ {
public class PodcastController : Controller public class PodcastController : Controller
{ {
public ArraySegment<Byte> CurrentOutput ;
public bool IsEnd;
ConcurrentBag<WebSocket> Listeners = new ConcurrentBag<WebSocket>();
public async Task Connect()
{
var socket = await this.HttpContext.WebSockets.AcceptWebSocketAsync();
}
} }
} }

@ -124,6 +124,10 @@ namespace Yavsc.Controllers
return View(); return View();
} }
public IActionResult VideoChat()
{
return View();
}
} }
} }

@ -1,15 +1,66 @@
using System;
using System.Collections.Concurrent;
using System.Net.WebSockets;
using System.Threading;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Hosting;
using Yavsc.Extensions;
namespace Yavsc namespace Yavsc
{ {
public partial class Startup public partial class Startup
{ {
public static ConcurrentBag<WebSocket> Listeners = new ConcurrentBag<WebSocket>();
public void ConfigureWebSocketsApp(IApplicationBuilder app, public void ConfigureWebSocketsApp(IApplicationBuilder app,
SiteSettings siteSettings, IHostingEnvironment env) SiteSettings siteSettings, IHostingEnvironment env)
{ {
app.UseWebSockets(); app.UseWebSockets();
app.UseSignalR("/api/signalr"); 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<byte>(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<byte>(buffer, 0, received.Count), received.MessageType, received.EndOfMessage, CancellationToken.None);
received = await webSocket.ReceiveAsync(new ArraySegment<byte>(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<WebSocket>(); var _sockets = new ConcurrentBag<WebSocket>();

Loading…