@@ -22,132 +22,198 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
SOFTWARE.
23
23
*/
24
24
25
- using UnityEngine ;
25
+ using UnityEngine ;
26
26
using System . Collections ;
27
27
using Moulin . DDP ;
28
+ using System ;
29
+ using UnityEngine . UI ;
30
+ using System . Collections . Generic ;
28
31
29
32
public class TestDdpClient : MonoBehaviour {
33
+ public Text DebugText ;
30
34
31
- public string serverUrl = "ws://localhost:3000/websocket" ;
35
+ public string serverUrl = "ws://localhost:3000/websocket" ;
32
36
public bool logMessages ;
33
37
34
38
private DdpConnection ddpConnection ;
39
+ private Queue < string > logQueue ;
35
40
36
- public void Start ( ) {
41
+ public void Start ( ) {
37
42
Application . runInBackground = true ; // Let the game run when the editor is not focused.
38
43
39
- ddpConnection = new DdpConnection ( serverUrl ) ;
44
+ this . DebugText . text = "" ;
45
+ logQueue = new Queue < string > ( ) ;
46
+ ddpConnection = new DdpConnection ( serverUrl ) ;
40
47
ddpConnection . logMessages = logMessages ;
48
+ ddpConnection . OnDebugMessage += AddDebugText ;
41
49
42
- ddpConnection . OnConnected += ( DdpConnection connection ) => {
43
- Debug . Log ( "Connected." ) ;
50
+ ddpConnection . OnConnected += ( DdpConnection connection ) => {
51
+ AddDebugText ( "Connected." ) ;
44
52
45
53
StartCoroutine ( MyCoroutine ( ) ) ;
46
54
} ;
47
55
48
56
ddpConnection . OnDisconnected += ( DdpConnection connection ) => {
49
- Debug . Log ( "Disconnected." ) ;
57
+ AddDebugText ( "Disconnected." ) ;
50
58
51
59
StartCoroutine ( CoroutineHelper . GetInstance ( ) . RunAfter ( ( ) => {
52
- Debug . Log ( "Try to reconnect ..." ) ;
60
+ AddDebugText ( "Try to reconnect ..." ) ;
53
61
connection . ConnectAsync ( ) ;
54
62
} , 2.0f ) ) ;
55
63
} ;
56
64
57
65
ddpConnection . OnConnectionClosed += ( DdpConnection connection ) => {
58
- Debug . Log ( "Connection closed." ) ;
66
+ AddDebugText ( "Connection closed." ) ;
59
67
} ;
60
68
61
69
ddpConnection . OnError += ( DdpError error ) => {
62
- Debug . Log ( "Error: " + error . errorCode + " " + error . reason ) ;
70
+ AddDebugText ( "Error: " + error . errorCode + " " + error . reason ) ;
63
71
} ;
64
72
65
73
ddpConnection . OnAdded += ( collection , id , fields ) => {
66
- Debug . Log ( "Added docId " + id +
74
+ AddDebugText ( "Added docId " + id +
67
75
" in collection " + collection ) ;
68
76
} ;
69
77
70
78
ddpConnection . OnRemoved += ( collection , id ) => {
71
- Debug . Log ( "Removed docId " + id +
79
+ AddDebugText ( "Removed docId " + id +
72
80
" in collection " + collection ) ;
73
81
} ;
74
82
75
83
ddpConnection . OnChanged += ( collection , id , fields , cleared ) => {
76
- Debug . Log ( "Changed docId " + id +
84
+ AddDebugText ( "Changed docId " + id +
77
85
" in collection " + collection +
78
86
" fields: " + fields +
79
87
" cleared:" + cleared ) ;
80
88
} ;
81
89
82
90
ddpConnection . OnAddedBefore += ( collection , id , fields , before ) => {
83
- Debug . Log ( "Added docId " + id +
91
+ AddDebugText ( "Added docId " + id +
84
92
" before docId " + before +
85
93
" in collection " + collection +
86
94
" fields: " + fields ) ;
87
95
} ;
88
96
89
97
ddpConnection . OnMovedBefore += ( collection , id , before ) => {
90
- Debug . Log ( "Moved docId " + id +
98
+ AddDebugText ( "Moved docId " + id +
91
99
" before docId " + before +
92
100
" in collection " + collection ) ;
93
101
} ;
94
102
95
103
}
96
104
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 ( ) ;
103
185
}
104
186
105
187
if ( Input . GetKeyDown ( KeyCode . V ) ) {
106
- Debug . Log ( "Closing connection ..." ) ;
107
- ddpConnection . Close ( ) ;
188
+ Disconnect ( ) ;
108
189
}
109
190
110
191
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 ( ) ;
115
193
}
116
194
117
195
if ( Input . GetKeyDown ( KeyCode . U ) ) {
118
- ddpConnection . Unsubscribe ( friendSub ) ;
196
+ Unsubscribe ( ) ;
119
197
}
120
198
121
199
if ( Input . GetKeyDown ( KeyCode . R ) ) {
122
- ddpConnection . Call ( "friends.removeAll" ) ;
200
+ RemoveAll ( ) ;
123
201
}
124
202
125
203
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 ( ) ;
133
205
}
134
206
135
207
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 ( ) ;
143
209
}
144
210
145
211
}
146
212
147
- private IEnumerator MyCoroutine ( ) {
213
+ private IEnumerator MyCoroutine ( ) {
148
214
MethodCall methodCall = ddpConnection . Call ( "friends.add" , JSONObject . Create ( 19 ) , JSONObject . Create ( 23 ) ) ;
149
215
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 ) ;
151
217
}
152
218
153
219
}
0 commit comments