Skip to content

Commit b5555fa

Browse files
committed
✨ Specify maxHosts for master server query
1 parent f2cf258 commit b5555fa

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ npm install steam-server-query
1313
## API
1414
### Master Server Query
1515
```javascript
16-
queryMasterServer(masterServer: string, region: REGIONS, filter?: Filter, timeout?: number): Promise<string[]>
16+
queryMasterServer(masterServer: string, region: REGIONS, filter?: Filter, timeout?: number, maxHosts?: number): Promise<string[]>
1717
```
1818
Fetch a Steam master server to retrieve a list of game server hosts.
1919
- `masterServer`: Host and port of the master server to call.
2020
- `region`: The region of the world where you wish to find servers in. Use `REGIONS.ALL` for all regions.
2121
- `filter`: Optional. Object which contains filters to be sent with the query. Default is { }.
2222
- `timeout`: Optional. Time in milliseconds after the socket request should fail. Default is 1 second.
23+
- `maxHosts`: Optional. Return a limited amount of hosts. Stops calling the master server after this limit is reached. Can be used to prevent getting rate limited.
2324
- Returns: A promise including an array of game server hosts.
2425
### Game Server Query
2526
```javascript

src/masterServer/masterServer.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ const RESPONSE_START = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF, 0x66, 0x0A]);
1212
* @param region The region of the world where you wish to find servers in. Use REGIONS.ALL for all regions.
1313
* @param filters Optional. Object which contains filters to be sent with the query. Default is { }. Read more [here](https://developer.valvesoftware.com/wiki/Master_Server_Query_Protocol#Filter).
1414
* @param timeout Optional. Time in milliseconds after the socket request should fail. Default is 1 second.
15+
* @param maxHosts Optional. Return a limited amount of hosts. Stops calling the master server after this limit is reached. Can be used to prevent getting rate limited.
1516
* @returns A promise including an array of game server hosts.
1617
*/
17-
export async function queryMasterServer(masterServer: string, region: REGIONS, filters: Filter = {}, timeout = 1000): Promise<string[]> {
18+
export async function queryMasterServer(masterServer: string, region: REGIONS, filters: Filter = {}, timeout = 1000, maxHosts?: number): Promise<string[]> {
1819
const splitMasterServer = masterServer.split(':');
1920
const host = splitMasterServer[0];
2021
const port = parseInt(splitMasterServer[1]);
2122

22-
const masterServerQuery = new MasterServerQuery(host, port, region, filters, timeout);
23+
const masterServerQuery = new MasterServerQuery(host, port, region, filters, timeout, maxHosts);
2324
const hosts = await masterServerQuery.fetchServers();
2425
return hosts;
2526
}
@@ -29,7 +30,7 @@ class MasterServerQuery {
2930
private _promiseSocket: PromiseSocket;
3031
private _hosts: string[] = [];
3132

32-
constructor(private _host: string, private _port: number, private _region: REGIONS, private _filters: Filter, timeout: number) {
33+
constructor(private _host: string, private _port: number, private _region: REGIONS, private _filters: Filter, timeout: number, private _maxHosts?: number) {
3334
this._promiseSocket = new PromiseSocket(1, timeout);
3435
};
3536

@@ -46,6 +47,14 @@ class MasterServerQuery {
4647
const parsedHosts = this._parseBuffer(resultBuffer);
4748
this._seedId = parsedHosts[parsedHosts.length - 1];
4849
this._hosts.push(...parsedHosts);
50+
51+
if (
52+
this._maxHosts &&
53+
this._hosts.length >= this._maxHosts &&
54+
this._hosts[this._maxHosts - 1] !== ZERO_IP
55+
) {
56+
return this._hosts.slice(0, this._maxHosts);
57+
}
4958
} while (this._seedId !== ZERO_IP);
5059

5160
// remove ZERO_IP from end of host list

0 commit comments

Comments
 (0)