@@ -69,8 +69,10 @@ public class Field {
69
69
70
70
public enum ConnectionState {
71
71
NOT_CONNECTED ,
72
- DISCONNECTED ,
72
+ CONNECTING ,
73
73
CONNECTED ,
74
+ DISCONNECTED ,
75
+ CLOSING ,
74
76
CLOSED
75
77
}
76
78
@@ -92,6 +94,7 @@ public enum ConnectionState {
92
94
93
95
public delegate void OnConnectedDelegate ( DdpConnection connection ) ;
94
96
public delegate void OnDisconnectedDelegate ( DdpConnection connection ) ;
97
+ public delegate void OnConnectionClosedDelegate ( DdpConnection connection ) ;
95
98
public delegate void OnAddedDelegate ( string collection , string docId , JSONObject fields ) ;
96
99
public delegate void OnChangedDelegate ( string collection , string docId , JSONObject fields , JSONObject cleared ) ;
97
100
public delegate void OnRemovedDelegate ( string collection , string docId ) ;
@@ -101,6 +104,7 @@ public enum ConnectionState {
101
104
102
105
public event OnConnectedDelegate OnConnected ;
103
106
public event OnDisconnectedDelegate OnDisconnected ;
107
+ public event OnConnectionClosedDelegate OnConnectionClosed ;
104
108
public event OnAddedDelegate OnAdded ;
105
109
public event OnChangedDelegate OnChanged ;
106
110
public event OnRemovedDelegate OnRemoved ;
@@ -146,8 +150,15 @@ private void OnWebSocketError(object sender, WebSocketSharp.ErrorEventArgs e) {
146
150
private void OnWebSocketClose ( object sender , WebSocketSharp . CloseEventArgs e ) {
147
151
if ( e . WasClean ) {
148
152
ddpConnectionState = ConnectionState . CLOSED ;
149
- }
150
- else {
153
+ sessionId = null ;
154
+ subscriptions . Clear ( ) ;
155
+ methodCalls . Clear ( ) ;
156
+ coroutineHelper . RunInMainThread ( ( ) => {
157
+ if ( OnDisconnected != null ) {
158
+ OnDisconnected ( this ) ;
159
+ }
160
+ } ) ;
161
+ } else {
151
162
ddpConnectionState = ConnectionState . DISCONNECTED ;
152
163
coroutineHelper . RunInMainThread ( ( ) => {
153
164
if ( OnDisconnected != null ) {
@@ -432,15 +443,19 @@ public ConnectionState GetConnectionState() {
432
443
}
433
444
434
445
public void Connect ( ) {
435
- ws . ConnectAsync ( ) ;
446
+ if ( ( ddpConnectionState == ConnectionState . NOT_CONNECTED ) ||
447
+ ( ddpConnectionState == ConnectionState . DISCONNECTED ) ||
448
+ ( ddpConnectionState == ConnectionState . CLOSED ) ) {
449
+ ddpConnectionState = ConnectionState . CONNECTING ;
450
+ ws . ConnectAsync ( ) ;
451
+ }
436
452
}
437
453
438
454
public void Close ( ) {
439
- ws . Close ( ) ;
440
-
441
- sessionId = null ;
442
- subscriptions . Clear ( ) ;
443
- methodCalls . Clear ( ) ;
455
+ if ( ddpConnectionState == ConnectionState . CONNECTED ) {
456
+ ddpConnectionState = ConnectionState . CLOSING ;
457
+ ws . Close ( ) ;
458
+ }
444
459
}
445
460
446
461
void IDisposable . Dispose ( ) {
0 commit comments