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

Commit 12cd090

Browse files
committed
Use Unity UI. Text and Buttons for debug
1 parent a747290 commit 12cd090

File tree

5 files changed

+4238
-138
lines changed

5 files changed

+4238
-138
lines changed

unity-project/Assets/example/TestDdpAccount.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,13 @@ public void Update() {
156156
{
157157
string log = logQueue.Dequeue();
158158
Debug.Log(log);
159-
DebugText.text = log + "\n" + DebugText.text;
159+
string[] original = DebugText.text.Split('\n');
160+
List<string> logMessages = new List<string>();
161+
for (int i = 0; i < Mathf.Min(original.Length, 10); i++)
162+
{
163+
logMessages.Add(original[i]);
164+
}
165+
DebugText.text = log + "\n" + string.Join("\n", logMessages);
160166
}
161167
}
162168

unity-project/Assets/example/TestDdpClient.cs

Lines changed: 111 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,132 +22,198 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424

25-
using UnityEngine;
25+
using UnityEngine;
2626
using System.Collections;
2727
using Moulin.DDP;
28+
using System;
29+
using UnityEngine.UI;
30+
using System.Collections.Generic;
2831

2932
public class TestDdpClient : MonoBehaviour {
33+
public Text DebugText;
3034

31-
public string serverUrl = "ws://localhost:3000/websocket";
35+
public string serverUrl = "ws://localhost:3000/websocket";
3236
public bool logMessages;
3337

3438
private DdpConnection ddpConnection;
39+
private Queue<string> logQueue;
3540

36-
public void Start() {
41+
public void Start() {
3742
Application.runInBackground = true; // Let the game run when the editor is not focused.
3843

39-
ddpConnection = new DdpConnection(serverUrl);
44+
this.DebugText.text = "";
45+
logQueue = new Queue<string>();
46+
ddpConnection = new DdpConnection(serverUrl);
4047
ddpConnection.logMessages = logMessages;
48+
ddpConnection.OnDebugMessage += AddDebugText;
4149

42-
ddpConnection.OnConnected += (DdpConnection connection) => {
43-
Debug.Log("Connected.");
50+
ddpConnection.OnConnected += (DdpConnection connection) => {
51+
AddDebugText("Connected.");
4452

4553
StartCoroutine(MyCoroutine());
4654
};
4755

4856
ddpConnection.OnDisconnected += (DdpConnection connection) => {
49-
Debug.Log("Disconnected.");
57+
AddDebugText("Disconnected.");
5058

5159
StartCoroutine(CoroutineHelper.GetInstance().RunAfter(() => {
52-
Debug.Log("Try to reconnect ...");
60+
AddDebugText("Try to reconnect ...");
5361
connection.ConnectAsync();
5462
}, 2.0f));
5563
};
5664

5765
ddpConnection.OnConnectionClosed += (DdpConnection connection) => {
58-
Debug.Log("Connection closed.");
66+
AddDebugText("Connection closed.");
5967
};
6068

6169
ddpConnection.OnError += (DdpError error) => {
62-
Debug.Log("Error: " + error.errorCode + " " + error.reason);
70+
AddDebugText("Error: " + error.errorCode + " " + error.reason);
6371
};
6472

6573
ddpConnection.OnAdded += (collection, id, fields) => {
66-
Debug.Log("Added docId " + id +
74+
AddDebugText("Added docId " + id +
6775
" in collection " + collection);
6876
};
6977

7078
ddpConnection.OnRemoved += (collection, id) => {
71-
Debug.Log("Removed docId " + id +
79+
AddDebugText("Removed docId " + id +
7280
" in collection " + collection);
7381
};
7482

7583
ddpConnection.OnChanged += (collection, id, fields, cleared) => {
76-
Debug.Log("Changed docId " + id +
84+
AddDebugText("Changed docId " + id +
7785
" in collection " + collection +
7886
" fields: " + fields +
7987
" cleared:" + cleared);
8088
};
8189

8290
ddpConnection.OnAddedBefore += (collection, id, fields, before) => {
83-
Debug.Log("Added docId " + id +
91+
AddDebugText("Added docId " + id +
8492
" before docId " + before +
8593
" in collection " + collection +
8694
" fields: " + fields);
8795
};
8896

8997
ddpConnection.OnMovedBefore += (collection, id, before) => {
90-
Debug.Log("Moved docId " + id +
98+
AddDebugText("Moved docId " + id +
9199
" before docId " + before +
92100
" in collection " + collection);
93101
};
94102

95103
}
96104

97-
private Subscription friendSub;
98-
99-
public void Update() {
100-
if (Input.GetKeyDown(KeyCode.C)) {
101-
Debug.Log("Connecting ...");
102-
ddpConnection.ConnectAsync();
105+
public void Connect()
106+
{
107+
AddDebugText("Connecting ...");
108+
ddpConnection.ConnectAsync();
109+
}
110+
111+
public void Disconnect()
112+
{
113+
AddDebugText("Closing connection ...");
114+
ddpConnection.Close();
115+
}
116+
117+
public void Subscribe()
118+
{
119+
friendSub = ddpConnection.Subscribe("friends");
120+
friendSub.OnReady = (Subscription obj) => {
121+
AddDebugText("Ready subscription: " + obj.id);
122+
};
123+
}
124+
125+
public void Unsubscribe()
126+
{
127+
ddpConnection.Unsubscribe(friendSub);
128+
}
129+
130+
public void RemoveAll()
131+
{
132+
ddpConnection.Call("friends.removeAll");
133+
}
134+
135+
public void CreateFriends()
136+
{
137+
MethodCall methodCall = ddpConnection.Call("friends.create", JSONObject.CreateStringObject("Coco"));
138+
methodCall.OnUpdated = (MethodCall obj) => {
139+
AddDebugText("Updated, methodId=" + obj.id);
140+
};
141+
methodCall.OnResult = (MethodCall obj) => {
142+
AddDebugText("Result = " + obj.result);
143+
};
144+
}
145+
146+
public void CallAdd()
147+
{
148+
MethodCall methodCall = ddpConnection.Call("friends.add", JSONObject.Create(19), JSONObject.Create(23));
149+
methodCall.OnUpdated = (MethodCall obj) => {
150+
AddDebugText("Updated, methodId=" + obj.id);
151+
};
152+
methodCall.OnResult = (MethodCall obj) => {
153+
AddDebugText("Result = " + obj.result);
154+
};
155+
}
156+
157+
private Subscription friendSub;
158+
159+
public void AddDebugText(string text)
160+
{
161+
logQueue.Enqueue(text);
162+
}
163+
164+
165+
public void Update()
166+
{
167+
if (logQueue != null)
168+
{
169+
while (logQueue.Count > 0)
170+
{
171+
string log = logQueue.Dequeue();
172+
Debug.Log(log);
173+
string[] original = DebugText.text.Split('\n');
174+
List<string> logMessages = new List<string>();
175+
for (int i = 0; i < Mathf.Min(original.Length, 10); i++)
176+
{
177+
logMessages.Add(original[i]);
178+
}
179+
DebugText.text = log + "\n" + string.Join("\n", logMessages);
180+
}
181+
}
182+
183+
if (Input.GetKeyDown(KeyCode.C)) {
184+
Connect();
103185
}
104186

105187
if (Input.GetKeyDown(KeyCode.V)) {
106-
Debug.Log("Closing connection ...");
107-
ddpConnection.Close();
188+
Disconnect();
108189
}
109190

110191
if (Input.GetKeyDown(KeyCode.S)) {
111-
friendSub = ddpConnection.Subscribe("friends");
112-
friendSub.OnReady = (Subscription obj) => {
113-
Debug.Log("Ready subscription: " + obj.id);
114-
};
192+
Subscribe();
115193
}
116194

117195
if (Input.GetKeyDown(KeyCode.U)) {
118-
ddpConnection.Unsubscribe(friendSub);
196+
Unsubscribe();
119197
}
120198

121199
if (Input.GetKeyDown(KeyCode.R)) {
122-
ddpConnection.Call("friends.removeAll");
200+
RemoveAll();
123201
}
124202

125203
if (Input.GetKeyDown(KeyCode.F)) {
126-
MethodCall methodCall = ddpConnection.Call("friends.create", JSONObject.CreateStringObject("Coco"));
127-
methodCall.OnUpdated = (MethodCall obj) => {
128-
Debug.Log("Updated, methodId=" + obj.id);
129-
};
130-
methodCall.OnResult = (MethodCall obj) => {
131-
Debug.Log("Result = " + obj.result);
132-
};
204+
CreateFriends();
133205
}
134206

135207
if (Input.GetKeyDown(KeyCode.A)) {
136-
MethodCall methodCall = ddpConnection.Call("friends.add", JSONObject.Create(7), JSONObject.Create(5));
137-
methodCall.OnUpdated = (MethodCall obj) => {
138-
Debug.Log("Updated, methodId=" + obj.id);
139-
};
140-
methodCall.OnResult = (MethodCall obj) => {
141-
Debug.Log("Result = " + obj.result);
142-
};
208+
CallAdd();
143209
}
144210

145211
}
146212

147-
private IEnumerator MyCoroutine() {
213+
private IEnumerator MyCoroutine() {
148214
MethodCall methodCall = ddpConnection.Call("friends.add", JSONObject.Create(19), JSONObject.Create(23));
149215
yield return methodCall.WaitForResult();
150-
Debug.Log("(19 + 23)'s call has a result: " + methodCall.result.i);
216+
AddDebugText("(19 + 23)'s call has a result: " + methodCall.result.i);
151217
}
152218

153219
}

0 commit comments

Comments
 (0)