More structed connection metadata

This commit is contained in:
moozzyk 2016-10-03 11:31:41 -07:00
parent 2b7a396670
commit 4cd4ddfad5
4 changed files with 45 additions and 1 deletions

View File

@ -12,6 +12,6 @@ namespace Microsoft.AspNetCore.Sockets
public string ConnectionId { get; set; }
public ClaimsPrincipal User { get; set; }
public IChannel Channel { get; set; }
public IDictionary<string, object> Metadata { get; } = new Dictionary<string, object>();
public ConnectionMetadata Metadata { get; } = new ConnectionMetadata();
}
}

View File

@ -0,0 +1,24 @@

using System.Collections.Generic;
namespace Microsoft.AspNetCore.Sockets
{
public class ConnectionMetadata
{
private IDictionary<string, object> _metadata = new Dictionary<string, object>();
public Format Format { get; set; } = Format.Text;
public object this[string key]
{
get
{
return _metadata[key];
}
set
{
_metadata[key] = value;
}
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Sockets
{
public enum Format
{
Text,
Binary
}
}

View File

@ -46,6 +46,10 @@ namespace Microsoft.AspNetCore.Sockets
// Get the end point mapped to this http connection
var endpoint = (EndPoint)context.RequestServices.GetRequiredService<TEndPoint>();
var format =
string.Equals(context.Request.Query["format"], "binary", StringComparison.OrdinalIgnoreCase)
? Format.Binary
: Format.Text;
// Server sent events transport
if (context.Request.Path.StartsWithSegments(path + "/sse"))
@ -54,6 +58,7 @@ namespace Microsoft.AspNetCore.Sockets
var connectionState = GetOrCreateConnection(context);
connectionState.Connection.User = context.User;
connectionState.Connection.Metadata["transport"] = "sse";
connectionState.Connection.Metadata.Format = format;
var sse = new ServerSentEvents(connectionState.Connection);
// Register this transport for disconnect
@ -82,6 +87,7 @@ namespace Microsoft.AspNetCore.Sockets
var connectionState = GetOrCreateConnection(context);
connectionState.Connection.User = context.User;
connectionState.Connection.Metadata["transport"] = "websockets";
connectionState.Connection.Metadata.Format = format;
var ws = new WebSockets(connectionState.Connection);
// Register this transport for disconnect
@ -136,6 +142,7 @@ namespace Microsoft.AspNetCore.Sockets
if (isNewConnection)
{
connectionState.Connection.Metadata["transport"] = "poll";
connectionState.Connection.Metadata.Format = format;
connectionState.Connection.User = context.User;
endpointTask = endpoint.OnConnected(connectionState.Connection);
connectionState.Connection.Metadata["endpoint"] = endpointTask;