1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . IO . Pipelines ;
44using System . Net . WebSockets ;
@@ -79,11 +79,22 @@ public class WebSocketManager //https://github.com/aspnet/AspNetCore/blob/ma
7979
8080 public WebSocketManager ( WebSocketTransport transport , CancellationToken cancel = default ) { Transport = transport ; Cancel = cancel ; }
8181
82- public async Task < WebSocket > AcceptWebSocketAsync ( string subprotocol ) //https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/WebSockets/src/WebSocketMiddleware.cs
82+ public async Task < WebSocket > AcceptWebSocketAsync ( string subprotocol , IDictionary < string , string > headers ) //https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/WebSockets/src/WebSocketMiddleware.cs
8383 {
8484 //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.
8585 var socket = new ClientWebSocket ( ) ;
86- await socket . ConnectAsync ( Transport . Url , Cancel ) ;
86+ foreach ( string key in headers . Keys )
87+ {
88+ socket . Options . SetRequestHeader ( key , headers [ key ] ) ;
89+ }
90+ socket . Options . KeepAliveInterval = new TimeSpan ( 1 , 1 , 1 ) ;
91+ try
92+ {
93+ await socket . ConnectAsync ( Transport . Url , Cancel ) ;
94+ }
95+ catch ( Exception e )
96+ {
97+ }
8798 return socket ;
8899 }
89100 }
@@ -144,8 +155,8 @@ public async Task ProcessRequestAsync(HttpContext context, CancellationToken tok
144155 Debug . Assert ( context . WebSockets . IsWebSocketRequest , "Not a websocket request" ) ;
145156
146157 var subProtocol = _options . SubProtocolSelector ? . Invoke ( context . WebSockets . WebSocketRequestedProtocols ) ;
147-
148- using ( var ws = await context . WebSockets . AcceptWebSocketAsync ( subProtocol ) )
158+ IDictionary < string , string > headers = _options . Headers != null ? _options . Headers ( ) : new Dictionary < string , string > ( ) ;
159+ using ( var ws = await context . WebSockets . AcceptWebSocketAsync ( subProtocol , headers ) )
149160 {
150161 Log . SocketOpened ( _logger , subProtocol ) ;
151162
@@ -531,6 +542,8 @@ public class WebSocketOptions
531542 // https://github.com/aspnet/HttpAbstractions/blob/a6bdb9b1ec6ed99978a508e71a7f131be7e4d9fb/src/Microsoft.AspNetCore.Http.Abstractions/WebSocketManager.cs#L23
532543 // Unfortunately, IList<T> does not implement IReadOnlyList<T> :(
533544 public Func < IList < string > , string > SubProtocolSelector { get ; set ; }
545+
546+ public Func < IDictionary < string , string > > Headers { get ; set ; }
534547 }
535548 #endregion
536549}
@@ -863,4 +876,4 @@ You may obtain a copy of the License at
863876 See the License for the specific language governing permissions and
864877 limitations under the License.
865878*/
866- #endregion
879+ #endregion
0 commit comments