@@ -16,8 +16,9 @@ public class WampSessionClient<TMessage> : IWampSessionClientExtended,
1616 private static readonly AuthenticateExtraData EmptyAuthenticateDetails = new AuthenticateExtraData ( ) ;
1717 private readonly IWampServerProxy mServerProxy ;
1818 private TaskCompletionSource < bool > mOpenTask = new TaskCompletionSource < bool > ( ) ;
19+ private TaskCompletionSource < GoodbyeMessage > mCloseTask ;
20+ private GoodbyeMessage mGoodbyeMessage ;
1921 private readonly IWampFormatter < TMessage > mFormatter ;
20- private readonly object mLock = new object ( ) ;
2122 private bool mGoodbyeSent ;
2223 private readonly IWampClientAuthenticator mAuthenticator ;
2324 private HelloDetails mSentDetails ;
@@ -115,6 +116,10 @@ public void Goodbye(GoodbyeDetails details, string reason)
115116 {
116117 mServerProxy . Goodbye ( new GoodbyeDetails ( ) , WampErrors . GoodbyeAndOut ) ;
117118 }
119+ else
120+ {
121+ mGoodbyeMessage = new GoodbyeMessage ( ) { Details = details , Reason = reason } ;
122+ }
118123 }
119124
120125 TrySetCloseEventArgs ( SessionCloseType . Goodbye , details , reason ) ;
@@ -126,11 +131,21 @@ private void RaiseConnectionBroken()
126131
127132 WampSessionCloseEventArgs closeEventArgs = mCloseEventArgs ;
128133
129- SetOpenTaskErrorIfNeeded ( new WampConnectionBrokenException ( mCloseEventArgs ) ) ;
130-
131134 Interlocked . CompareExchange ( ref mIsConnected , 0 , 1 ) ;
135+
136+ GoodbyeMessage goodbyeMessage = mGoodbyeMessage ;
137+
138+ if ( goodbyeMessage != null )
139+ {
140+ mCloseTask ? . SetResult ( goodbyeMessage ) ;
141+ }
142+
143+ SetTasksErrorsIfNeeded ( new WampConnectionBrokenException ( mCloseEventArgs ) ) ;
144+
132145 mOpenTask = new TaskCompletionSource < bool > ( ) ;
146+ mCloseTask = null ;
133147 mCloseEventArgs = null ;
148+ mGoodbyeMessage = null ;
134149
135150 OnConnectionBroken ( closeEventArgs ) ;
136151 }
@@ -154,13 +169,20 @@ private void TrySetCloseEventArgs(SessionCloseType sessionCloseType,
154169
155170 public Task OpenTask => mOpenTask . Task ;
156171
157- public void Close ( string reason , GoodbyeDetails details )
172+ public Task < GoodbyeMessage > Close ( string reason , GoodbyeDetails details )
158173 {
159174 reason = reason ?? WampErrors . CloseNormal ;
160175 details = details ?? EmptyGoodbyeDetails ;
161176
177+ TaskCompletionSource < GoodbyeMessage > closeTask =
178+ new TaskCompletionSource < GoodbyeMessage > ( ) ;
179+
180+ mCloseTask = closeTask ;
181+
162182 mGoodbyeSent = true ;
163183 mServerProxy . Goodbye ( details , reason ) ;
184+
185+ return closeTask . Task ;
164186 }
165187
166188 public void OnConnectionOpen ( )
@@ -191,19 +213,15 @@ public void OnConnectionClosed()
191213
192214 public void OnConnectionError ( Exception exception )
193215 {
194- SetOpenTaskErrorIfNeeded ( exception ) ;
216+ SetTasksErrorsIfNeeded ( exception ) ;
195217
196218 OnConnectionError ( new WampConnectionErrorEventArgs ( exception ) ) ;
197219 }
198220
199- private void SetOpenTaskErrorIfNeeded ( Exception exception )
221+ private void SetTasksErrorsIfNeeded ( Exception exception )
200222 {
201- TaskCompletionSource < bool > openTask = mOpenTask ;
202-
203- if ( openTask != null )
204- {
205- openTask . TrySetException ( exception ) ;
206- }
223+ mOpenTask ? . TrySetException ( exception ) ;
224+ mCloseTask ? . TrySetException ( exception ) ;
207225 }
208226
209227 public event EventHandler < WampSessionCreatedEventArgs > ConnectionEstablished ;
0 commit comments