From 239b5f815fa604ca7adcc3ac6a386458c6f35e83 Mon Sep 17 00:00:00 2001 From: moozzyk Date: Fri, 30 Sep 2016 15:13:21 -0700 Subject: [PATCH] Fixing longpolling --- src/WebApplication95/ConnectionState.cs | 3 +- src/WebApplication95/Dispatcher.cs | 15 ++-- src/WebApplication95/LongPolling.cs | 19 ++--- src/WebApplication95/wwwroot/index.html | 4 +- src/WebApplication95/wwwroot/polling.html | 88 ++++++++++++++--------- 5 files changed, 69 insertions(+), 60 deletions(-) diff --git a/src/WebApplication95/ConnectionState.cs b/src/WebApplication95/ConnectionState.cs index b615c798ff..d273a9bdff 100644 --- a/src/WebApplication95/ConnectionState.cs +++ b/src/WebApplication95/ConnectionState.cs @@ -2,10 +2,11 @@ namespace WebApplication95 { - public class ConnectionState + public class ConnectionState { public DateTimeOffset LastSeen { get; set; } public bool Alive { get; set; } = true; public Connection Connection { get; set; } + public bool IsReservation { get; set; } } } diff --git a/src/WebApplication95/Dispatcher.cs b/src/WebApplication95/Dispatcher.cs index ca419e94ad..ed96e97b25 100644 --- a/src/WebApplication95/Dispatcher.cs +++ b/src/WebApplication95/Dispatcher.cs @@ -58,6 +58,7 @@ namespace WebApplication95 if (context.Request.Path.StartsWithSegments(path + "/sse")) { var connectionState = GetOrCreateConnection(context); + connectionState.IsReservation = false; var sse = new ServerSentEvents(connectionState); var ignore = endpoint.OnConnected(connectionState.Connection); @@ -73,6 +74,8 @@ namespace WebApplication95 else if (context.Request.Path.StartsWithSegments(path + "/ws")) { var connectionState = GetOrCreateConnection(context); + connectionState.IsReservation = false; + var ws = new WebSockets(connectionState); var ignore = endpoint.OnConnected(connectionState.Connection); @@ -89,18 +92,17 @@ namespace WebApplication95 { var connectionId = context.Request.Query["id"]; ConnectionState connectionState; - bool newConnection = false; - if (_manager.AddConnection(connectionId, out connectionState)) - { - newConnection = true; - var ignore = endpoint.OnConnected(connectionState.Connection); + if (_manager.AddConnection(connectionId, out connectionState) || connectionState.IsReservation) + { connectionState.Connection.TransportType = TransportType.LongPolling; + connectionState.IsReservation = false; + var ignore = endpoint.OnConnected(connectionState.Connection); } var longPolling = new LongPolling(connectionState); - await longPolling.ProcessRequest(newConnection, context); + await longPolling.ProcessRequest(context); _manager.MarkConnectionDead(connectionState.Connection.ConnectionId); } @@ -112,6 +114,7 @@ namespace WebApplication95 var connectionId = _manager.GetConnectionId(context); ConnectionState state; _manager.AddConnection(connectionId, out state); + state.IsReservation = true; context.Response.Headers["X-SignalR-ConnectionId"] = connectionId; await context.Response.WriteAsync($"{{ \"connectionId\": \"{connectionId}\" }}"); return; diff --git a/src/WebApplication95/LongPolling.cs b/src/WebApplication95/LongPolling.cs index b47e9654ee..9b32eaaace 100644 --- a/src/WebApplication95/LongPolling.cs +++ b/src/WebApplication95/LongPolling.cs @@ -37,10 +37,8 @@ namespace WebApplication95 return _lastTask; } - public async Task ProcessRequest(bool newConnection, HttpContext context) + public async Task ProcessRequest(HttpContext context) { - context.Response.ContentType = "application/json"; - // End the connection if the client goes away context.RequestAborted.Register(state => OnConnectionAborted(state), this); @@ -48,17 +46,8 @@ namespace WebApplication95 _initTcs.TrySetResult(null); - if (newConnection) - { - // Flush the connection id to the connection - var ignore = Send(default(ArraySegment)); - } - else - { - // Send queue messages to the connection - var ignore = ProcessMessages(context); - } - + // Send queue messages to the connection + var ignore = ProcessMessages(context); await _lifetime.Task; @@ -106,7 +95,7 @@ namespace WebApplication95 var data = ((ArraySegment)state); _context.Response.Headers["X-SignalR-ConnectionId"] = _state.Connection.ConnectionId; _context.Response.ContentLength = data.Count; - await _context.Response.Body.WriteAsync(data.Array, 0, data.Count); + await _context.Response.Body.WriteAsync(data.Array, data.Offset, data.Count); }, value); diff --git a/src/WebApplication95/wwwroot/index.html b/src/WebApplication95/wwwroot/index.html index 20e02eaa7d..07b1f786f6 100644 --- a/src/WebApplication95/wwwroot/index.html +++ b/src/WebApplication95/wwwroot/index.html @@ -9,19 +9,17 @@ var xhr = new XMLHttpRequest(); var url = '/chat/send?id=' + connectionId; xhr.open("POST", url, true); - xhr.setRequestHeader('Content-type', 'application/json'); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { } } - var data = JSON.stringify(data); xhr.send(data); } function xhr(method, url) { return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest(); - xhr.open(method, url); + xhr.open(method, url, true); xhr.send(); xhr.onload = () => { if (xhr.status >= 200 && xhr.status < 300) { diff --git a/src/WebApplication95/wwwroot/polling.html b/src/WebApplication95/wwwroot/polling.html index 29a5e4b792..dbac867b2a 100644 --- a/src/WebApplication95/wwwroot/polling.html +++ b/src/WebApplication95/wwwroot/polling.html @@ -4,57 +4,75 @@

Long Polling

- +