@hcikn/colibri 1.0.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,9 +8,13 @@
8
8
  ## Configuration
9
9
 
10
10
  Initialize Colibri once with:
11
+
11
12
  ```ts
12
13
  import { Colibri } from '@hcikn/colibri';
13
- Colibri.init('app_name', 'server_address');
14
+ new Colibri('app_name', 'server_address');
15
+
16
+ // if using a custom port:
17
+ new Colibri('app_name', 'server_address', 9011);
14
18
  ```
15
19
 
16
20
  For server setup, refer to [colibri-server](../colibri-server/).
@@ -25,7 +29,11 @@ To setup, import the `RemoteLogger` and call its `init()` method. Any subsequent
25
29
 
26
30
  ```ts
27
31
  import { RemoteLogger } from '@hcikn/colibri';
28
- RemoteLogger.init();
32
+ const logger = new RemoteLogger();
33
+
34
+ // en-/disable RemoteLogger
35
+ logger.enable();
36
+ logger.disable();
29
37
  ```
30
38
 
31
39
  See also [the remote-logging sample](samples/remote-logging.ts) (run sample with `npm run sample/remote-logging`).
@@ -34,12 +42,14 @@ See also [the remote-logging sample](samples/remote-logging.ts) (run sample with
34
42
 
35
43
  Colibri supports simple data transmission via pub/sub communication. Data can be published from anywhere in
36
44
  the executed code, as illustrated with the following simple example of sending a boolean value on a "click" channel:
45
+
37
46
  ```ts
38
47
  import { Sync } from '@hcikn/colibri';
39
48
  Sync.sendBool('myChannel', true);
40
49
  ```
41
50
 
42
51
  The sent data can then be received anywhere by registering a listener:
52
+
43
53
  ```ts
44
54
  Sync.receiveBool('myChannel', (value) => {
45
55
  // Will be called whenever a bool on "click" channel is received
@@ -47,11 +57,12 @@ Sync.receiveBool('myChannel', (value) => {
47
57
  ```
48
58
 
49
59
  The listener can be deregistered via:
60
+
50
61
  ```ts
51
62
  Sync.unregister('myChannel', MyMethod);
52
63
  ```
53
64
 
54
- The following built-in types are available for sync: `bool, int (as number), float (as number), string, Vector2, Vector3, Quaternion, Color` and arrays thereof. For arbitrary data, you can use JSON:
65
+ The following built-in types are available for sync: `bool, int (as number), float (as number), string, Vector2, Vector3, Quaternion, Color` and arrays thereof. For arbitrary data, you can use JSON:
55
66
 
56
67
  ```c#
57
68
  Sync.sendJson('myChannel', { foo: 'bar' });
@@ -61,14 +72,16 @@ Sync.receiveJson('myChannel', (json) => { /* ... */ });
61
72
  See also [the broadcast sample](samples/broadcast.ts) (run sample with `npm run sample/broadcast`).
62
73
 
63
74
  Limitations:
64
- - You have to register the listener *before* sending out data
65
- - Type and channel *must* match between Listener and Sender (`number` will be converted to float for Unity clients, i.e., use `float` listener on Unity clients for sending numbers!)
75
+
76
+ - You have to register the listener _before_ sending out data
77
+ - Type and channel _must_ match between Listener and Sender (`number` will be converted to float for Unity clients, i.e., use `float` listener on Unity clients for sending numbers!)
66
78
  - Remember to unregister your listener where necessary!
67
79
 
68
80
  ### SyncModel
81
+
69
82
  (Counterpart to [SyncBehaviour in Unity client](../colibri-unity#SyncBehaviour))
70
83
 
71
- For more complex scenarios, Colibri supports synchronization of data models (e.g., for use in model-view-controller architectures). For this, we need extend the *Model* with `SyncModel`:
84
+ For more complex scenarios, Colibri supports synchronization of data models (e.g., for use in model-view-controller architectures). For this, we need extend the _Model_ with `SyncModel`:
72
85
 
73
86
  ```ts
74
87
  import { SyncModel, Synced } from '@hcikn/colibri';
@@ -88,6 +101,7 @@ export class SampleClass extends SyncModel<SampleClass> {
88
101
  ```
89
102
 
90
103
  and enable `experimentalDecorators` in the `tsconfig.json`:
104
+
91
105
  ```json
92
106
  {
93
107
  "compilerOptions": {
@@ -100,10 +114,12 @@ and enable `experimentalDecorators` in the `tsconfig.json`:
100
114
  Any property or field marked with `@Synced()` will be synchronized across all network clients. *Note: Synchronization of fields (e.g., `@Synced() private age = 0;` does not work on some frameworks such as React for some reason.*
101
115
 
102
116
  Lastly, we need to register the class with the Synchronization mechanism by calling `RegisterModelSync`:
117
+
103
118
  ```ts
104
119
  import { RegisterModelSync } from '@hcikn/colibri';
105
120
  const [ SampleClasses$, registerExampleClass ] = RegisterModelSync<SampleClass>({ type: SampleClass });
106
121
  ```
122
+
107
123
  The first return value (e.g., `SampleClasses$`) is a [BehaviorSubject](https://www.learnrxjs.io/learn-rxjs/subjects/behaviorsubject) that will be updated whenever a new instance of SampleClass is added, updated, or deleted. The second return value (e.g., `registerExampleClass`) can be used to sync new instantiations:
108
124
 
109
125
  ```ts
@@ -113,5 +129,28 @@ registerExampleClass(mySample); // mySample is sent out to all other clients and
113
129
 
114
130
  See also [the model-sync sample](samples/model-sync.ts) (run sample with `npm run sample/model-sync`).
115
131
 
132
+ ### Remote Store
133
+
134
+ Colibri offers persistent data storage on the server, so that data can be shared easily between connected clients. Each object is identified by it's individual `key`:
135
+
136
+ ```ts
137
+ import { Colibri, GetRestApi, PutRestApi } from '@hcikn/colibri';
138
+
139
+ const colibri = Colibri.getInstance();
140
+ const key = 'sampleKey';
141
+ const data = {...};
142
+
143
+ // fetch data
144
+ colibri.getRestObject(key); // or...
145
+ GetRestApi(key);
146
+
147
+ // update data
148
+ colibri.setRestObject(key, data); // or...
149
+ PutRestApi(key, data);
150
+ ```
151
+
152
+ Each operation can be achieved by either directly interaction with the `Colibri` object or using the corresponding wrapper method.
153
+
116
154
  ## Samples
155
+
117
156
  See [Sample folder](samples/) for more examples on how to use the Colibri web client.