Better error handling with web sockets

This commit is contained in:
Frank A. Krueger 2018-04-15 17:40:48 -07:00
parent 92e9b9bee4
commit f9a0fd732a
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
2 changed files with 30 additions and 10 deletions

View File

@ -3,6 +3,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Ooui.AspNetCore
{
@ -10,10 +11,12 @@ namespace Ooui.AspNetCore
{
readonly Element element;
readonly string title;
readonly bool disposeWhenDone;
public ElementResult (Element element, string title = "", bool disposeWhenDone = true)
{
readonly bool disposeWhenDone;
readonly ILogger logger;
public ElementResult (Element element, string title = "", bool disposeWhenDone = true, ILogger logger = null)
{
this.logger = logger;
this.element = element;
this.title = title;
this.disposeWhenDone = disposeWhenDone;

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.IO;
using Microsoft.Extensions.Logging;
namespace Ooui.AspNetCore
{
@ -33,9 +35,7 @@ namespace Ooui.AspNetCore
return id;
}
public static async Task HandleWebSocketRequestAsync (HttpContext context)
public static async Task HandleWebSocketRequestAsync (HttpContext context, ILogger logger = null)
{
void BadRequest (string message)
{
@ -97,9 +97,26 @@ namespace Ooui.AspNetCore
// OK, Run
//
var token = CancellationToken.None;
var webSocket = await context.WebSockets.AcceptWebSocketAsync ("ooui");
var session = new Ooui.WebSocketSession (webSocket, activeSession.Element, activeSession.DisposeElementWhenDone, w, h, token);
await session.RunAsync ().ConfigureAwait (false);
System.Net.WebSockets.WebSocket webSocket = null;
//
// Create a new session and let it handle everything from here
//
try {
webSocket = await context.WebSockets.AcceptWebSocketAsync ("ooui").ConfigureAwait (false);
var session = new Ooui.WebSocketSession (webSocket, activeSession.Element, activeSession.DisposeElementWhenDone, w, h, token);
await session.RunAsync ().ConfigureAwait (false);
}
catch (System.Net.WebSockets.WebSocketException ex) when (ex.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely) {
// The remote party closed the WebSocket connection without completing the close handshake.
}
catch (Exception ex) {
context.Abort ();
Console.WriteLine (ex);
}
finally {
webSocket?.Dispose ();
}
}
class PendingSession