@hcikn/colibri 1.0.2

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 ADDED
@@ -0,0 +1,117 @@
1
+ # Colibri - Web Client
2
+
3
+ ## Installation
4
+
5
+ - NPM: `npm install @hcikn/colibri`
6
+ - Yarn: `yarn add @hcikn/colibri`
7
+
8
+ ## Configuration
9
+
10
+ Initialize Colibri once with:
11
+ ```ts
12
+ import { Colibri } from '@hcikn/colibri';
13
+ Colibri.initialize('app_name', 'server_address');
14
+ ```
15
+
16
+ For server setup, refer to [colibri-server](../colibri-server/).
17
+
18
+ ## Usage
19
+
20
+ ### Web Interface for Logging
21
+
22
+ Colibri provides a _web logger_ with web interface to send diagnostic data (currently: console logs) to the server. This may be useful for devices (e.g., VR devices, smartphones) where access to the console is not easily available.
23
+
24
+ To setup, import the `RemoteLogger` and call its `init()` method. Any subsequent `console` calls should now also appear on your colibri server's web interface, which can be accessed via `http://<your-server-ip>:9011`.
25
+
26
+ ```ts
27
+ import { RemoteLogger } from '@hcikn/colibri';
28
+ RemoteLogger.init();
29
+ ```
30
+
31
+ See also [the remote-logging sample](samples/remote-logging.ts) (run sample with `npm run sample/remote-logging`).
32
+
33
+ ### Sending Data between Clients
34
+
35
+ Colibri supports simple data transmission via pub/sub communication. Data can be published from anywhere in
36
+ the executed code, as illustrated with the following simple example of sending a boolean value on a "click" channel:
37
+ ```ts
38
+ import { Sync } from '@hcikn/colibri';
39
+ Sync.sendBool('myChannel', true);
40
+ ```
41
+
42
+ The sent data can then be received anywhere by registering a listener:
43
+ ```ts
44
+ Sync.receiveBool('myChannel', (value) => {
45
+ // Will be called whenever a bool on "click" channel is received
46
+ });
47
+ ```
48
+
49
+ The listener can be deregistered via:
50
+ ```ts
51
+ Sync.unregister('myChannel', MyMethod);
52
+ ```
53
+
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:
55
+
56
+ ```c#
57
+ Sync.sendJson('myChannel', { foo: 'bar' });
58
+ Sync.receiveJson('myChannel', (json) => { /* ... */ });
59
+ ```
60
+
61
+ See also [the broadcast sample](samples/broadcast.ts) (run sample with `npm run sample/broadcast`).
62
+
63
+ 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!)
66
+ - Remember to unregister your listener where necessary!
67
+
68
+ ### SyncModel
69
+ (Counterpart to [SyncBehaviour in Unity client](../colibri-unity#SyncBehaviour))
70
+
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`:
72
+
73
+ ```ts
74
+ import { SyncModel, Synced } from '@hcikn/colibri';
75
+
76
+ export class SampleClass extends SyncModel<SampleClass> {
77
+ @Synced()
78
+ get name() { return this._name; }
79
+ set name(val: string) { this._name = val; }
80
+ private _name = '';
81
+
82
+ @Synced()
83
+ private age = 0;
84
+
85
+ @Synced('billingAddress')
86
+ private address = '';
87
+ }
88
+ ```
89
+
90
+ and enable `experimentalDecorators` in the `tsconfig.json`:
91
+ ```json
92
+ {
93
+ "compilerOptions": {
94
+ "experimentalDecorators": true
95
+ }
96
+ }
97
+
98
+ ```
99
+
100
+ 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
+
102
+ Lastly, we need to register the class with the Synchronization mechanism by calling `RegisterModelSync`:
103
+ ```ts
104
+ import { RegisterModelSync } from '@hcikn/colibri';
105
+ const [ SampleClasses$, registerExampleClass ] = RegisterModelSync<SampleClass>({ type: SampleClass });
106
+ ```
107
+ 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
+
109
+ ```ts
110
+ const mySample = new SampleClass('myId'); // mySample is not synchronized across clients yet
111
+ registerExampleClass(mySample); // mySample is sent out to all other clients and will be synchronized
112
+ ```
113
+
114
+ See also [the model-sync sample](samples/model-sync.ts) (run sample with `npm run sample/model-sync`).
115
+
116
+ ## Samples
117
+ See [Sample folder](samples/) for more examples on how to use the Colibri web client.