File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const RustPlus = require ( '@liamcottle/rustplus.js' ) ;
2+ var rustplus = new RustPlus ( 'ip' , 'port' , 'playerId' , 'playerToken' ) ;
3+
4+ // wait until connected before sending commands
5+ rustplus . on ( 'connected' , ( ) => {
6+
7+ /**
8+ * sendRequestAsync will return a promise for your request.
9+ * you can optionally pass in a second parameter for the timeout in milliseconds
10+ * - AppResponse packets will be sent to `then` on success.
11+ * - AppError packets and Timeout Errors will be sent to `catch`.
12+ */
13+ rustplus . sendRequestAsync ( {
14+ getInfo : { } , // get server info with a timeout of 2 seconds
15+ } , 2000 ) . then ( ( response ) => {
16+
17+ // AppResponse
18+ console . log ( response ) ;
19+
20+ } ) . catch ( ( error ) => {
21+
22+ // AppError or Error('Timeout');
23+ console . log ( error ) ;
24+
25+ } ) . finally ( ( ) => {
26+
27+ // disconnect so our script is finished
28+ rustplus . disconnect ( ) ;
29+
30+ } ) ;
31+
32+ } ) ;
33+
34+ // connect to rust server
35+ rustplus . connect ( ) ;
Original file line number Diff line number Diff line change @@ -150,6 +150,42 @@ class RustPlus extends EventEmitter {
150150
151151 }
152152
153+ /**
154+ * Send a Request to the Rust Server and return a Promise
155+ * @param data this should contain valid data for the AppRequest packet defined in the rustplus.proto schema file
156+ * @param timeoutMilliseconds milliseconds before the promise will be rejected. Defaults to 10 seconds.
157+ */
158+ sendRequestAsync ( data , timeoutMilliseconds = 10000 ) {
159+ return new Promise ( ( resolve , reject ) => {
160+
161+ // reject promise after timeout
162+ var timeout = setTimeout ( ( ) => {
163+ reject ( new Error ( 'Timeout reached while waiting for response' ) ) ;
164+ } , timeoutMilliseconds ) ;
165+
166+ // send request
167+ this . sendRequest ( data , ( message ) => {
168+
169+ // cancel timeout
170+ clearTimeout ( timeout ) ;
171+
172+ if ( message . response . error ) {
173+
174+ // reject promise if server returns an AppError for this request
175+ reject ( message . response . error ) ;
176+
177+ } else {
178+
179+ // request was successful, resolve with message.response
180+ resolve ( message . response ) ;
181+
182+ }
183+
184+ } ) ;
185+
186+ } ) ;
187+ }
188+
153189 /**
154190 * Send a Request to the Rust Server to set the Entity Value.
155191 * @param entityId the entity id to set the value for
You can’t perform that action at this time.
0 commit comments