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

Commit c2cae80

Browse files
author
Vincent Cantin
committed
Added localDB features, and reorganised the folder structure.
1 parent c94851c commit c2cae80

30 files changed

+651
-6
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class TestLocalDB : MonoBehaviour {
5+
6+
public string serverUrl = "ws://localhost:3000/websocket";
7+
public bool logMessages;
8+
9+
private DdpConnection ddpConnection;
10+
private LocalDB localDB;
11+
private JsonObjectCollection friendCollection;
12+
13+
public void Start() {
14+
ddpConnection = new DdpConnection(serverUrl);
15+
ddpConnection.logMessages = logMessages;
16+
17+
ddpConnection.OnConnected += (DdpConnection connection) => {
18+
Debug.Log("Connected.");
19+
};
20+
21+
ddpConnection.OnDisconnected += (DdpConnection connection) => {
22+
Debug.Log("Disconnected.");
23+
24+
StartCoroutine(CoroutineHelper.GetInstance().RunAfter(() => {
25+
Debug.Log("Try to reconnect ...");
26+
connection.Connect();
27+
}, 2.0f));
28+
};
29+
30+
ddpConnection.OnError += (DdpError error) => {
31+
Debug.Log("Error: " + error.errorCode + " " + error.reason);
32+
};
33+
34+
localDB = new LocalDB((db, collectionName) => {
35+
return new JsonObjectCollection(db, collectionName);
36+
}, ddpConnection);
37+
38+
friendCollection = (JsonObjectCollection) localDB.GetCollection("friends");
39+
friendCollection.OnAdded += (id, fields) => {
40+
Debug.Log("Added docId " + id);
41+
};
42+
43+
friendCollection.OnRemoved += (id) => {
44+
Debug.Log("Removed docId " + id);
45+
};
46+
47+
friendCollection.OnChanged += (id, fields, cleared) => {
48+
Debug.Log("Changed docId " + id +
49+
" fields: " + fields +
50+
" cleared:" + cleared);
51+
};
52+
}
53+
54+
private Subscription friendSub;
55+
56+
public void Update() {
57+
if (Input.GetKeyDown(KeyCode.C)) {
58+
Debug.Log("Connecting ...");
59+
ddpConnection.Connect();
60+
}
61+
62+
if (Input.GetKeyDown(KeyCode.V)) {
63+
ddpConnection.Close();
64+
}
65+
66+
if (Input.GetKeyDown(KeyCode.S)) {
67+
friendSub = ddpConnection.Subscribe("friends");
68+
friendSub.OnReady = (Subscription obj) => {
69+
Debug.Log("Ready subscription: " + obj.id);
70+
};
71+
}
72+
73+
if (Input.GetKeyDown(KeyCode.U)) {
74+
ddpConnection.Unsubscribe(friendSub);
75+
}
76+
77+
if (Input.GetKeyDown(KeyCode.R)) {
78+
ddpConnection.Call("friends.removeAll");
79+
}
80+
81+
if (Input.GetKeyDown(KeyCode.F)) {
82+
MethodCall methodCall = ddpConnection.Call("friends.create", JSONObject.CreateStringObject("Coco"));
83+
methodCall.OnUpdated = (MethodCall obj) => {
84+
Debug.Log("Updated, methodId=" + obj.id);
85+
};
86+
methodCall.OnResult = (MethodCall obj) => {
87+
Debug.Log("Result = " + obj.result);
88+
};
89+
}
90+
91+
if (Input.GetKeyDown(KeyCode.D)) {
92+
foreach (var entry in friendCollection.documents) {
93+
Debug.Log(entry.Key + " " + entry.Value);
94+
}
95+
}
96+
97+
}
98+
99+
}

unity-project/Assets/example/TestLocalDB.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!29 &1
4+
SceneSettings:
5+
m_ObjectHideFlags: 0
6+
m_PVSData:
7+
m_PVSObjectsArray: []
8+
m_PVSPortalsArray: []
9+
m_OcclusionBakeSettings:
10+
smallestOccluder: 5
11+
smallestHole: 0.25
12+
backfaceThreshold: 100
13+
--- !u!104 &2
14+
RenderSettings:
15+
m_ObjectHideFlags: 0
16+
serializedVersion: 6
17+
m_Fog: 0
18+
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
19+
m_FogMode: 3
20+
m_FogDensity: 0.01
21+
m_LinearFogStart: 0
22+
m_LinearFogEnd: 300
23+
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
24+
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
25+
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
26+
m_AmbientIntensity: 1
27+
m_AmbientMode: 3
28+
m_SkyboxMaterial: {fileID: 0}
29+
m_HaloStrength: 0.5
30+
m_FlareStrength: 1
31+
m_FlareFadeSpeed: 3
32+
m_HaloTexture: {fileID: 0}
33+
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
34+
m_DefaultReflectionMode: 0
35+
m_DefaultReflectionResolution: 128
36+
m_ReflectionBounces: 1
37+
m_ReflectionIntensity: 1
38+
m_CustomReflection: {fileID: 0}
39+
m_Sun: {fileID: 0}
40+
--- !u!157 &3
41+
LightmapSettings:
42+
m_ObjectHideFlags: 0
43+
serializedVersion: 6
44+
m_GIWorkflowMode: 1
45+
m_LightmapsMode: 1
46+
m_GISettings:
47+
serializedVersion: 2
48+
m_BounceScale: 1
49+
m_IndirectOutputScale: 1
50+
m_AlbedoBoost: 1
51+
m_TemporalCoherenceThreshold: 1
52+
m_EnvironmentLightingMode: 0
53+
m_EnableBakedLightmaps: 0
54+
m_EnableRealtimeLightmaps: 0
55+
m_LightmapEditorSettings:
56+
serializedVersion: 3
57+
m_Resolution: 2
58+
m_BakeResolution: 40
59+
m_TextureWidth: 1024
60+
m_TextureHeight: 1024
61+
m_AOMaxDistance: 1
62+
m_Padding: 2
63+
m_CompAOExponent: 0
64+
m_LightmapParameters: {fileID: 0}
65+
m_TextureCompression: 1
66+
m_FinalGather: 0
67+
m_FinalGatherRayCount: 1024
68+
m_ReflectionCompression: 2
69+
m_LightingDataAsset: {fileID: 0}
70+
m_RuntimeCPUUsage: 25
71+
--- !u!196 &4
72+
NavMeshSettings:
73+
serializedVersion: 2
74+
m_ObjectHideFlags: 0
75+
m_BuildSettings:
76+
serializedVersion: 2
77+
agentRadius: 0.5
78+
agentHeight: 2
79+
agentSlope: 45
80+
agentClimb: 0.4
81+
ledgeDropHeight: 0
82+
maxJumpAcrossDistance: 0
83+
accuratePlacement: 0
84+
minRegionArea: 2
85+
cellSize: 0.16666667
86+
manualCellSize: 0
87+
m_NavMeshData: {fileID: 0}
88+
--- !u!1 &327968603
89+
GameObject:
90+
m_ObjectHideFlags: 0
91+
m_PrefabParentObject: {fileID: 0}
92+
m_PrefabInternal: {fileID: 0}
93+
serializedVersion: 4
94+
m_Component:
95+
- 4: {fileID: 327968608}
96+
- 20: {fileID: 327968607}
97+
- 92: {fileID: 327968606}
98+
- 124: {fileID: 327968605}
99+
- 81: {fileID: 327968604}
100+
m_Layer: 0
101+
m_Name: Main Camera
102+
m_TagString: MainCamera
103+
m_Icon: {fileID: 0}
104+
m_NavMeshLayer: 0
105+
m_StaticEditorFlags: 0
106+
m_IsActive: 1
107+
--- !u!81 &327968604
108+
AudioListener:
109+
m_ObjectHideFlags: 0
110+
m_PrefabParentObject: {fileID: 0}
111+
m_PrefabInternal: {fileID: 0}
112+
m_GameObject: {fileID: 327968603}
113+
m_Enabled: 1
114+
--- !u!124 &327968605
115+
Behaviour:
116+
m_ObjectHideFlags: 0
117+
m_PrefabParentObject: {fileID: 0}
118+
m_PrefabInternal: {fileID: 0}
119+
m_GameObject: {fileID: 327968603}
120+
m_Enabled: 1
121+
--- !u!92 &327968606
122+
Behaviour:
123+
m_ObjectHideFlags: 0
124+
m_PrefabParentObject: {fileID: 0}
125+
m_PrefabInternal: {fileID: 0}
126+
m_GameObject: {fileID: 327968603}
127+
m_Enabled: 1
128+
--- !u!20 &327968607
129+
Camera:
130+
m_ObjectHideFlags: 0
131+
m_PrefabParentObject: {fileID: 0}
132+
m_PrefabInternal: {fileID: 0}
133+
m_GameObject: {fileID: 327968603}
134+
m_Enabled: 1
135+
serializedVersion: 2
136+
m_ClearFlags: 1
137+
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844}
138+
m_NormalizedViewPortRect:
139+
serializedVersion: 2
140+
x: 0
141+
y: 0
142+
width: 1
143+
height: 1
144+
near clip plane: 0.3
145+
far clip plane: 1000
146+
field of view: 60
147+
orthographic: 1
148+
orthographic size: 5
149+
m_Depth: -1
150+
m_CullingMask:
151+
serializedVersion: 2
152+
m_Bits: 4294967295
153+
m_RenderingPath: -1
154+
m_TargetTexture: {fileID: 0}
155+
m_TargetDisplay: 0
156+
m_TargetEye: 3
157+
m_HDR: 0
158+
m_OcclusionCulling: 1
159+
m_StereoConvergence: 10
160+
m_StereoSeparation: 0.022
161+
m_StereoMirrorMode: 0
162+
--- !u!4 &327968608
163+
Transform:
164+
m_ObjectHideFlags: 0
165+
m_PrefabParentObject: {fileID: 0}
166+
m_PrefabInternal: {fileID: 0}
167+
m_GameObject: {fileID: 327968603}
168+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
169+
m_LocalPosition: {x: 0, y: 0, z: -10}
170+
m_LocalScale: {x: 1, y: 1, z: 1}
171+
m_Children: []
172+
m_Father: {fileID: 0}
173+
m_RootOrder: 0
174+
--- !u!1 &810825833
175+
GameObject:
176+
m_ObjectHideFlags: 0
177+
m_PrefabParentObject: {fileID: 0}
178+
m_PrefabInternal: {fileID: 0}
179+
serializedVersion: 4
180+
m_Component:
181+
- 4: {fileID: 810825835}
182+
- 114: {fileID: 810825834}
183+
m_Layer: 0
184+
m_Name: testLocalDB
185+
m_TagString: Untagged
186+
m_Icon: {fileID: 0}
187+
m_NavMeshLayer: 0
188+
m_StaticEditorFlags: 0
189+
m_IsActive: 1
190+
--- !u!114 &810825834
191+
MonoBehaviour:
192+
m_ObjectHideFlags: 0
193+
m_PrefabParentObject: {fileID: 0}
194+
m_PrefabInternal: {fileID: 0}
195+
m_GameObject: {fileID: 810825833}
196+
m_Enabled: 1
197+
m_EditorHideFlags: 0
198+
m_Script: {fileID: 11500000, guid: 648d04024ae1b424fa00aa49ab9465e8, type: 3}
199+
m_Name:
200+
m_EditorClassIdentifier:
201+
serverUrl: ws://localhost:3000/websocket
202+
logMessages: 0
203+
--- !u!4 &810825835
204+
Transform:
205+
m_ObjectHideFlags: 0
206+
m_PrefabParentObject: {fileID: 0}
207+
m_PrefabInternal: {fileID: 0}
208+
m_GameObject: {fileID: 810825833}
209+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
210+
m_LocalPosition: {x: 0, y: 0, z: 0}
211+
m_LocalScale: {x: 1, y: 1, z: 1}
212+
m_Children: []
213+
m_Father: {fileID: 0}
214+
m_RootOrder: 1

unity-project/Assets/example/test-local-db.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unity-project/Assets/unity3d-ddp-client/account.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unity-project/Assets/example/DdpAccount.cs renamed to unity-project/Assets/unity3d-ddp-client/account/DdpAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private void HandleLoginResult(MethodCall loginCall) {
3838
if (error == null) {
3939
JSONObject result = loginCall.result;
4040
isLogged = true;
41-
this.username = username;
41+
this.username = result["username"].str;
4242
this.userId = result["id"].str;
4343
this.token = result["token"].str;
4444
this.tokenExpiration = result["tokenExpires"].GetDateTime();

unity-project/Assets/unity3d-ddp-client/ddp.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using System.Collections;
55
using System.Collections.Generic;
66

7+
/*
8+
* DDP protocol:
9+
* https://github.com/meteor/meteor/blob/master/packages/ddp/DDP.md
10+
*/
711
public class DdpConnection : IDisposable {
812

913
// The possible values for the "msg" field.
@@ -86,11 +90,11 @@ public enum ConnectionState {
8690

8791
public delegate void OnConnectedDelegate(DdpConnection connection);
8892
public delegate void OnDisconnectedDelegate(DdpConnection connection);
89-
public delegate void OnAddedDelegate(string collection, string id, JSONObject fields);
90-
public delegate void OnChangedDelegate(string collection, string id, JSONObject fields, JSONObject cleared);
91-
public delegate void OnRemovedDelegate(string collection, string id);
92-
public delegate void OnAddedBeforeDelegate(string collection, string id, JSONObject fields, string before);
93-
public delegate void OnMovedBeforeDelegate(string collection, string id, string before);
93+
public delegate void OnAddedDelegate(string collection, string docId, JSONObject fields);
94+
public delegate void OnChangedDelegate(string collection, string docId, JSONObject fields, JSONObject cleared);
95+
public delegate void OnRemovedDelegate(string collection, string docId);
96+
public delegate void OnAddedBeforeDelegate(string collection, string docId, JSONObject fields, string before);
97+
public delegate void OnMovedBeforeDelegate(string collection, string docId, string before);
9498
public delegate void OnErrorDelegate(DdpError error);
9599

96100
public event OnConnectedDelegate OnConnected;

0 commit comments

Comments
 (0)