bf4rcon
v1.0.1
Published
BF4 Rcon in pure JavaScript
Downloads
1
Readme
skuIIs/bf-rcon
BF4 RCon in pure JavaScript
Installation
You can use npm or yarn to install this package into your project
npm install bf4rcon
yarn add bf4rcon
Usage
const { BFrcon } = require('bf4rcon')
async function init () {
const rcon = new BFrcon()
// Connects to server(s), can be an object or an array of objects
// *** Example for a single server ***
const server = await rcon.connect({
host: '127.0.0.1',
port: 47200,
password: 'password'
})
server.on('ready', async function () {
console.log('Server: ' + this.server_id + ' added to listener!')
// Run some command
const players = await this.listPlayers()
console.log(players)
// Listen for some event
this.on('player.onAuthenticated', function (player) {
console.log(player)
})
}.bind(server))
// *** Example for a multiple servers ***
const servers = await rcon.connect([
{
host: '127.0.0.1',
port: 47200,
password: 'password'
},
{
host: '127.0.0.1',
port: 47201,
password: 'password'
},
])
for (var i = 0, len = servers.length; i < len; i++) {
servers[i].on('ready', async function () {
console.log('Server: ' + this.server_id + ' added to listener!')
// Run some command
const players = await this.listPlayers()
console.log(players)
// Listen for some event
this.on('player.onAuthenticated', function (player) {
console.log(player)
})
}.bind(servers[i]))
}
}
init()
Commands
After connecting to a server(s) you can use the exec
function to execute a command, or use the built-in API for commands made with promises. See ./services/commands/*
for available commands.
...
this.exec('serverInfo', function (error, response) {
if (error) return reject(error)
// Do something with response. Note here you must parse things yourself!
})
...
Is the same as:
...
const info = await this.serverInfo()
// Already parsed for you!
console.log(info)
...
Events
Listen for events from the server is simple. After connecting to the server(s) just use a standard on
listener.
this.on('player.onChat', function (chat) {
console.log(chat)
})
Caveats
This has only been tested for BF4. This could work for BF3 or BFH.
This package is meant for long-running processes and also doesn't handle reconnecting after disconnects or errors. You must handle those on your own. Example:
const config = {
host: '127.0.0.1',
port: 47200,
password: 'password'
}
// Initial connection
const server = await rcon.connect(config)
server.on('ready', async function () {
// Code
}.bind(server))
// Socket connection was ended
server.on('end', function () {
// Reconnect!
server = await rcon.connect(config)
})
- If you want to issue a command on a server and exit then it can be done, you just have to connect, issue the command after the connection is ready, and then manually disconnect. Example:
const server = await rcon.connect({
host: '127.0.0.1',
port: 47200,
password: 'password'
})
server.on('ready', async function () {
// Run some command
const players = await this.listPlayers()
console.log(players)
// Disconnect
this.disconnect()
}.bind(server))
Modules
Classes
Geo
Geo Helper module
module.exports(ip) ⇒ String | Null ⏏
Get the country from an IP address
Kind: Exported function
Returns: String | Null - returns a string of the country if found or null
Throws:
- TypeError throws if ip is not a string
| Param | Type | Description | | --- | --- | --- | | ip | String | IP address to lookup |
Map
Map Helper module
module.exports(map) ⇒ String | Null ⏏
Converts rcon map to full map string
Kind: Exported function
Returns: String | Null - returns full map string or null if not found
Throws:
- TypeError throws if map is not a string
| Param | Type | Description | | --- | --- | --- | | map | String | rcon map to convert |
Mode
Mode Helper module
module.exports(mode) ⇒ String | Null ⏏
Converts rcon mode to full mode string
Kind: Exported function
Returns: String | Null - returns full mode string or null if not found
Throws:
- TypeError throws if mode is not a string
| Param | Type | Description | | --- | --- | --- | | mode | String | rcon mode to convert |
Weapon
Weapon Helper module
- Weapon
- module.exports(weapon) ⇒ Object.<WeaponObject> | Null ⏏
- ~WeaponObject : Object
- module.exports(weapon) ⇒ Object.<WeaponObject> | Null ⏏
module.exports(weapon) ⇒ Object.<WeaponObject> | Null ⏏
Converts rcon weapon to full weapon object
Kind: Exported function
Returns: Object.<WeaponObject> | Null - returns full weapon object or null if not found
Throws:
- TypeError throws if weapon is not a string
| Param | Type | Description | | --- | --- | --- | | weapon | String | rcon mode to convert |
module.exports~WeaponObject : Object
Kind: inner typedef of module.exports
Properties
| Name | Type | Description | | --- | --- | --- | | weapon_key | String | | | type | String | [primary, secondary, auxiliary] | | kit | String | [assault, engineer, support, recon, none] | | group | String | [suicide, melee, impact, nonlethal, assaultrifle, carbine, smg, lmg, shotgun, dmr, sniperrifle, handgun, explosive, projectileexplosive, vehiclepersonal, vehicletransport, vehiclestationary, vehiclelight, vehicleheavy, vehicleair, vehiclewater, none] |
BFRcon
BFRcon class
Kind: global class
- BFRcon
- instance
- static
- "player.onAuthenticated" ⇒ Object
- "player.onJoin" ⇒ Object
- "player.onLeave" ⇒ Object
- "player.onDisconnect" ⇒ Object
- "player.onSpawn" ⇒ Object
- "player.onKill" ⇒ Object
- "player.onChat" ⇒ Object
- "player.onSquadChange" ⇒ Object
- "player.onTeamChange" ⇒ Object
- "server.onMaxPlayerCountChange" ⇒ Object
- "server.onLevelLoaded" ⇒ Object
- "server.onRoundOver" ⇒ Object
- "server.onRoundOverPlayers" ⇒ Object
- "server.onRoundOverTeamScores" ⇒ Object
- "punkbuster.onComputed" ⇒ Object
- "punkbuster.onViolation" ⇒ Object
bfRcon.connect(servers) ⇒ Object
Connects to server(s)
Kind: instance method of BFRcon
| Param | Type | Description | | --- | --- | --- | | servers | Array | Object | an array of servers or a single server to connect to |
bfRcon.exec(command, callback)
Executes a command
Kind: instance method of BFRcon
| Param | Type | Description | | --- | --- | --- | | command | String | command to execute | | callback | function | callback |
"player.onAuthenticated" ⇒ Object
Player authenticated event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, refresh: Boolean}
"player.onJoin" ⇒ Object
Player join event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, guid: String, refresh: Boolean}
"player.onLeave" ⇒ Object
Player leave event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, info: String, refresh: Boolean}
"player.onDisconnect" ⇒ Object
Player disconnect event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, refresh: Boolean}
"player.onSpawn" ⇒ Object
Player spawn event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, team: Number, refresh: Boolean}
"player.onKill" ⇒ Object
Player kill event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, victim: String, weapon: {weapon_key: String, type: String, kit: String, group: String}, headshot: Boolean, refresh: Boolean}
"player.onChat" ⇒ Object
Player chat event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, text: String, subset: String, refresh: Boolean}
"player.onSquadChange" ⇒ Object
Player squad change event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, team: Number, squad: Number, refresh: Boolean}
"player.onTeamChange" ⇒ Object
Player team change event
Kind: event emitted by BFRcon
Returns: Object - {event: String, name: String, team: Number, squad: Number, refresh: Boolean}
"server.onMaxPlayerCountChange" ⇒ Object
Server max player count change event
Kind: event emitted by BFRcon
Returns: Object - {event: String, count: Number, refresh: Boolean}
"server.onLevelLoaded" ⇒ Object
Server level loaded event
Kind: event emitted by BFRcon
Returns: Object - {event: String, level: String, mode: String, played: Number, total: Number, refresh: Boolean}
"server.onRoundOver" ⇒ Object
Server round over event
Kind: event emitted by BFRcon
Returns: Object - {event: String, winner: Number, refresh: Boolean}
"server.onRoundOverPlayers" ⇒ Object
Server round over players event
Kind: event emitted by BFRcon
Returns: Object - {event: String, players: Array, refresh: Boolean}
"server.onRoundOverTeamScores" ⇒ Object
Server round over team scores event
Kind: event emitted by BFRcon
Returns: Object - {event: String, scores: Array, refresh: Boolean}
"punkbuster.onComputed" ⇒ Object
Server punkbuster computed event
Kind: event emitted by BFRcon
Returns: Object - {event: String, message: String, name: String, guid: String, ip: String, country: String, refresh: Boolean}
"punkbuster.onViolation" ⇒ Object
Server punkbuster violation event
Kind: event emitted by BFRcon
Returns: Object - {event: String, message: String, name: String, guid: String, ip: String, country: String, type: String, number: Number, refresh: Boolean}
Contributing
Pull requests are welcome for bug fixes or feature requests.
Sponsors
Support this project and possibly other open-source projects by becoming a sponsor. Higher tier sponsor will appear here with a logo and link to your website. Become a sponsor