[TRY]
This commit is contained in:
parent
ef83ada9c5
commit
f14b8b0598
3 changed files with 57 additions and 10 deletions
|
|
@ -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<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();
|
||||
}
|
||||
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.Hosting;
|
||||
using Yavsc.Extensions;
|
||||
|
||||
namespace Yavsc
|
||||
{
|
||||
public partial class Startup
|
||||
{
|
||||
public static ConcurrentBag<WebSocket> Listeners = new ConcurrentBag<WebSocket>();
|
||||
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<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>();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue