Skip to content

Provides WebSocket Headers Settings #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions RSocket.Core/Transports/WebSocketTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Net.WebSockets;
Expand Down Expand Up @@ -79,11 +79,22 @@ public class WebSocketManager //https://github.com/aspnet/AspNetCore/blob/ma

public WebSocketManager(WebSocketTransport transport, CancellationToken cancel = default) { Transport = transport; Cancel = cancel; }

public async Task<WebSocket> AcceptWebSocketAsync(string subprotocol) //https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/WebSockets/src/WebSocketMiddleware.cs
public async Task<WebSocket> AcceptWebSocketAsync(string subprotocol, IDictionary<string, string> headers) //https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/WebSockets/src/WebSocketMiddleware.cs
{
//So in the SignalR code, this is where the WebSocketOptions are actually applied. So junky, so overabstracted. This is why the constructors are inverted so short out all of this madness.
var socket = new ClientWebSocket();
await socket.ConnectAsync(Transport.Url, Cancel);
foreach (string key in headers.Keys)
{
socket.Options.SetRequestHeader(key, headers[key]);
}
socket.Options.KeepAliveInterval = new TimeSpan(1, 1, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be included in the PR since it is unrelated

try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be wrapped in try-catch since the exception is swallowed silently

{
await socket.ConnectAsync(Transport.Url, Cancel);
}
catch (Exception e)
{
}
return socket;
}
}
Expand Down Expand Up @@ -144,8 +155,8 @@ public async Task ProcessRequestAsync(HttpContext context, CancellationToken tok
Debug.Assert(context.WebSockets.IsWebSocketRequest, "Not a websocket request");

var subProtocol = _options.SubProtocolSelector?.Invoke(context.WebSockets.WebSocketRequestedProtocols);

using (var ws = await context.WebSockets.AcceptWebSocketAsync(subProtocol))
IDictionary<string, string> headers = _options.Headers != null ? _options.Headers() : new Dictionary<string, string>();
using (var ws = await context.WebSockets.AcceptWebSocketAsync(subProtocol, headers))
{
Log.SocketOpened(_logger, subProtocol);

Expand Down Expand Up @@ -531,6 +542,8 @@ public class WebSocketOptions
// https://github.com/aspnet/HttpAbstractions/blob/a6bdb9b1ec6ed99978a508e71a7f131be7e4d9fb/src/Microsoft.AspNetCore.Http.Abstractions/WebSocketManager.cs#L23
// Unfortunately, IList<T> does not implement IReadOnlyList<T> :(
public Func<IList<string>, string> SubProtocolSelector { get; set; }

public Func<IDictionary<string, string>> Headers { get; set; }
}
#endregion
}
Expand Down Expand Up @@ -863,4 +876,4 @@ You may obtain a copy of the License at
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion
#endregion