forked from liamcottle/rustplus.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_entity_changed_broadcasts.js
More file actions
58 lines (42 loc) · 1.72 KB
/
4_entity_changed_broadcasts.js
File metadata and controls
58 lines (42 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const RustPlus = require('@liamcottle/rustplus.js');
var rustplus = new RustPlus('ip', 'port', 'playerId', 'playerToken');
// wait until connected before sending commands
rustplus.on('connected', () => {
/**
* getting an entities info will cause the rust server
* to send an entity changed broadcast each time the
* entities state changes.
*/
rustplus.getEntityInfo(1234567, (message) => {
/**
* you can of course see the current entity state in this callback
* as a 'one off' check. But broadcasts will still be sent.
*/
console.log("getEntityInfo result: " + JSON.stringify(message));
/**
* we won't disconnect from the rust server in this example
* as we want to receive broadcasts in the message handler below.
*/
// rustplus.disconnect();
});
});
// listen for messages from rust server
rustplus.on('message', (message) => {
// check if message is an entity changed broadcast
if(message.broadcast && message.broadcast.entityChanged){
/**
* since we called getEntityInfo, this message handler will be triggered
* when the entity state has changed. for example, when a smart alarm is triggered,
* a smart switch is toggled or a storage monitor has updated.
*/
var entityChanged = message.broadcast.entityChanged;
// log the broadcast
console.log(message.broadcast);
var entityId = entityChanged.entityId;
var value = entityChanged.payload.value;
// log the entity status
console.log("entity " + entityId + " is now " + (value ? "active" : "inactive"));
}
});
// connect to rust server
rustplus.connect();