Skip to content
This repository was archived by the owner on Feb 20, 2022. It is now read-only.

Commit 95c1e7f

Browse files
author
Vincent Cantin
committed
Fixes #3 : rewrote the connection states and their transitions.
1 parent 18f7b0d commit 95c1e7f

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

unity-project/Assets/example/TestDdpClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public void Start() {
2828
}, 2.0f));
2929
};
3030

31+
ddpConnection.OnConnectionClosed += (DdpConnection connection) => {
32+
Debug.Log("Connection closed.");
33+
};
34+
3135
ddpConnection.OnError += (DdpError error) => {
3236
Debug.Log("Error: " + error.errorCode + " " + error.reason);
3337
};
@@ -73,6 +77,7 @@ public void Update() {
7377
}
7478

7579
if (Input.GetKeyDown(KeyCode.V)) {
80+
Debug.Log("Closing connection ...");
7681
ddpConnection.Close();
7782
}
7883

unity-project/Assets/unity3d-ddp-client/3rd/websocket-sharp/websocket-sharp.csproj.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

unity-project/Assets/unity3d-ddp-client/ddp/DdpConnection.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ public class Field {
6969

7070
public enum ConnectionState {
7171
NOT_CONNECTED,
72-
DISCONNECTED,
72+
CONNECTING,
7373
CONNECTED,
74+
DISCONNECTED,
75+
CLOSING,
7476
CLOSED
7577
}
7678

@@ -92,6 +94,7 @@ public enum ConnectionState {
9294

9395
public delegate void OnConnectedDelegate(DdpConnection connection);
9496
public delegate void OnDisconnectedDelegate(DdpConnection connection);
97+
public delegate void OnConnectionClosedDelegate(DdpConnection connection);
9598
public delegate void OnAddedDelegate(string collection, string docId, JSONObject fields);
9699
public delegate void OnChangedDelegate(string collection, string docId, JSONObject fields, JSONObject cleared);
97100
public delegate void OnRemovedDelegate(string collection, string docId);
@@ -101,6 +104,7 @@ public enum ConnectionState {
101104

102105
public event OnConnectedDelegate OnConnected;
103106
public event OnDisconnectedDelegate OnDisconnected;
107+
public event OnConnectionClosedDelegate OnConnectionClosed;
104108
public event OnAddedDelegate OnAdded;
105109
public event OnChangedDelegate OnChanged;
106110
public event OnRemovedDelegate OnRemoved;
@@ -146,8 +150,15 @@ private void OnWebSocketError(object sender, WebSocketSharp.ErrorEventArgs e) {
146150
private void OnWebSocketClose(object sender, WebSocketSharp.CloseEventArgs e) {
147151
if (e.WasClean) {
148152
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 {
151162
ddpConnectionState = ConnectionState.DISCONNECTED;
152163
coroutineHelper.RunInMainThread(() => {
153164
if (OnDisconnected != null) {
@@ -432,15 +443,19 @@ public ConnectionState GetConnectionState() {
432443
}
433444

434445
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+
}
436452
}
437453

438454
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+
}
444459
}
445460

446461
void IDisposable.Dispose() {

0 commit comments

Comments
 (0)