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 System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Ooui.AspNetCore namespace Ooui.AspNetCore
{ {
@ -11,9 +12,11 @@ namespace Ooui.AspNetCore
readonly Element element; readonly Element element;
readonly string title; readonly string title;
readonly bool disposeWhenDone; readonly bool disposeWhenDone;
readonly ILogger logger;
public ElementResult (Element element, string title = "", bool disposeWhenDone = true) public ElementResult (Element element, string title = "", bool disposeWhenDone = true, ILogger logger = null)
{ {
this.logger = logger;
this.element = element; this.element = element;
this.title = title; this.title = title;
this.disposeWhenDone = disposeWhenDone; this.disposeWhenDone = disposeWhenDone;

View File

@ -4,6 +4,8 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System.IO;
using Microsoft.Extensions.Logging;
namespace Ooui.AspNetCore namespace Ooui.AspNetCore
{ {
@ -33,9 +35,7 @@ namespace Ooui.AspNetCore
return id; return id;
} }
public static async Task HandleWebSocketRequestAsync (HttpContext context, ILogger logger = null)
public static async Task HandleWebSocketRequestAsync (HttpContext context)
{ {
void BadRequest (string message) void BadRequest (string message)
{ {
@ -97,9 +97,26 @@ namespace Ooui.AspNetCore
// OK, Run // OK, Run
// //
var token = CancellationToken.None; var token = CancellationToken.None;
var webSocket = await context.WebSockets.AcceptWebSocketAsync ("ooui"); System.Net.WebSockets.WebSocket webSocket = null;
var session = new Ooui.WebSocketSession (webSocket, activeSession.Element, activeSession.DisposeElementWhenDone, w, h, token);
await session.RunAsync ().ConfigureAwait (false); //
// 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 class PendingSession