@@ -124,18 +124,8 @@ await transport.WebSocket.SendAsync(segments,
124124 }
125125 }
126126
127- public async Task ConnectAsync ( WebSocket webSocket , string connectorName = "" )
127+ public async Task ConnectAsync ( WebSocket webSocket , string connectionId , string connectorName = "" )
128128 {
129- WebSocketTransport transport = new WebSocketTransport ( webSocket , connectorName ) ;
130- var connectionId = transport . ConnectionId ;
131- var context = new WebSocketMessageContext ( ) ;
132- context . Command = WebSocketCommands . Handshake ;
133- context . Value = connectionId ;
134- context . Header = await _initState . GetStateAsync ( ) ;
135- Connections . TryAdd ( connectionId , transport ) ;
136-
137- await SendAsync ( connectionId , context ) ;
138-
139129 var receiverContext = new WebSocketReceiverContext
140130 {
141131 Compressor = _compressor ,
@@ -145,6 +135,24 @@ public async Task ConnectAsync(WebSocket webSocket, string connectorName = "")
145135 Options = _options ,
146136 WebSocket = webSocket
147137 } ;
138+
139+ WebSocketTransport transport = null ;
140+ if ( Connections . TryGetValue ( connectionId , out transport ) )
141+ {
142+ transport . ReConnect ( webSocket ) ;
143+ }
144+ else
145+ {
146+ transport = new WebSocketTransport ( webSocket , connectionId , connectorName ) ;
147+ var context = new WebSocketMessageContext ( ) ;
148+ context . Command = WebSocketCommands . Handshake ;
149+ context . Value = connectionId ;
150+ context . Header = await _initState . GetStateAsync ( ) ;
151+ Connections . TryAdd ( connectionId , transport ) ;
152+
153+ await SendAsync ( connectionId , context ) ;
154+ }
155+
148156 var receiver = new WebSocketReceiver ( receiverContext , CloseConnection ) ;
149157 await receiver . ReceiveAsync ( ) ;
150158 }
@@ -281,18 +289,34 @@ await transport.WebSocket.SendAsync(segments,
281289 }
282290 }
283291
284- public void CloseConnection ( string connectionId )
292+ public void CloseConnection ( string connectionId , bool keepAlive )
285293 {
286294 WebSocketTransport transport = null ;
287- if ( Connections . TryRemove ( connectionId , out transport ) )
295+ if ( keepAlive )
288296 {
289- transport . Dispose ( ) ;
297+ if ( Connections . TryGetValue ( connectionId , out transport ) )
298+ {
299+ transport . Dispose ( ) ;
300+ }
301+ }
302+ else
303+ {
304+ if ( Connections . TryRemove ( connectionId , out transport ) )
305+ {
306+ transport . Dispose ( ) ;
307+ }
290308 }
291309 }
292310
293311 public void CloseConnection ( WebSocketReceiverContext context )
294312 {
295- CloseConnection ( context . ConnectionId ) ;
313+ bool keepAlive = false ;
314+ if ( context . WebSocket . CloseStatus == WebSocketCloseStatus . EndpointUnavailable )
315+ {
316+ keepAlive = true ;
317+ }
318+
319+ CloseConnection ( context . ConnectionId , keepAlive ) ;
296320 }
297321 }
298322}
0 commit comments