This repository was archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFleckWebSocketConnectionMiddleware.cs
More file actions
64 lines (54 loc) · 2.16 KB
/
Copy pathFleckWebSocketConnectionMiddleware.cs
File metadata and controls
64 lines (54 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Fleck;
using Microsoft.Owin;
using Microsoft.Practices.ServiceLocation;
namespace Owin.WebSocket
{
public class FleckWebSocketConnectionMiddleware<T> : OwinMiddleware
where T : FleckWebSocketConnection, new()
{
private readonly Action<IWebSocketConnection> _config;
private readonly Action<IOwinWebSocketConnection> _owinConfig;
private readonly IServiceLocator _locator;
private readonly Regex _matchPattern;
public FleckWebSocketConnectionMiddleware(OwinMiddleware next, Action<IWebSocketConnection> config, IServiceLocator locator, Regex matchPattern)
: this(next, locator, matchPattern)
{
_config = config;
}
public FleckWebSocketConnectionMiddleware(OwinMiddleware next, Action<IOwinWebSocketConnection> config, IServiceLocator locator, Regex matchPattern)
: this(next, locator, matchPattern)
{
_owinConfig = config;
}
private FleckWebSocketConnectionMiddleware(OwinMiddleware next, IServiceLocator locator, Regex matchPattern)
: base(next)
{
_locator = locator;
_matchPattern = matchPattern;
}
public override Task Invoke(IOwinContext context)
{
var matches = new Dictionary<string, string>();
if (_matchPattern != null)
{
var match = _matchPattern.Match(context.Request.Path.Value);
if (!match.Success)
return Next.Invoke(context);
for (var i = 1; i <= match.Groups.Count; i++)
{
var name = _matchPattern.GroupNameFromNumber(i);
var value = match.Groups[i];
matches.Add(name, value.Value);
}
}
var connection = _locator?.GetInstance<T>() ?? new T();
_config?.Invoke(connection);
_owinConfig?.Invoke(connection);
return connection.AcceptSocketAsync(context, matches);
}
}
}