client: make console connection logging optional

This commit is contained in:
extremeheat 2021-12-31 15:16:18 +00:00
commit 49fd2b69ee
3 changed files with 11 additions and 5 deletions

View file

@ -13,12 +13,12 @@ Returns a `Client` instance and connects to the server.
| version | *optional* | Version to connect as. If not specified, automatically match server version. |
| offline | *optional* | default to **false**. Set this to true to disable Microsoft/Xbox auth. |
| username | Conditional | Required if `offline` set to true : Username to connect to server as. |
| authTitle | *optional* | The title ID to connect as, see the README for usage. |
| connectTimeout | *optional* | default to **9000ms**. How long to wait in milliseconds while trying to connect to server. |
| onMsaCode | *optional* | Callback called when signing in with a microsoft account with device code auth, `data` is an object documented [here](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code#device-authorization-response) |
| profilesFolder | *optional* | Where to store cached authentication tokens. Defaults to .minecraft, or the node_modules folder if not found. |
| autoInitPlayer | *optional* | default to true, If we should send SetPlayerInitialized to the server after getting play_status spawn. |
| skipPing | *optional* | Whether pinging the server to check its version should be skipped. |
| conLog | *optional* | Where to log connection information (server join, kick messages to). Defaults to console.log, set to `null` to not log anywhere. |
| useNativeRaknet | *optional* | Whether to use the C++ version of RakNet. Set to false to use JS. |
| authTitle | *optional* | The client ID to sign in as, defaults to Minecraft for Nintendo Switch. Set false to sign in through Azure. See prismarine-auth |
| deviceType | *optional* | The device type to sign in as, defaults to "Nintendo". See prismarine-auth |
@ -48,6 +48,7 @@ authenticated unless offline is set to true.
| kickTimeout | *[Future][1]* | How long to wait before kicking a unresponsive client. |
| motd | *optional* | The "message of the day" for the server, the message shown to players in the server list. See usage below. |
| advertisementFn | *optional* | optional. Custom function to call that should return a ServerAdvertisement, used for setting the RakNet server PONG data. Overrides `motd`. |
| conLog | *optional* | Where to log connection information (server join, kick messages to). Default to log only in DEBUG mode. |
## be.ping({ host, port }) : ServerAdvertisement

View file

@ -41,6 +41,7 @@ class Client extends Connection {
this.inLog = (...args) => debug('C ->', ...args)
this.outLog = (...args) => debug('C <-', ...args)
}
this.conLog = this.options.conLog === undefined ? console.log : this.options.conLog
}
connect () {
@ -82,7 +83,9 @@ class Client extends Connection {
try {
return await this.connection.ping(this.options.connectTimeout)
} catch (e) {
console.warn(`Unable to connect to [${this.options.host}]/${this.options.port}. Is the server running?`)
// TODO: workaround bug in standardjs, waiting for https://github.com/standard/eslint-config-standard/pull/193
const t = `Unable to connect to [${this.options.host}]/${this.options.port}. Is the server running?`
this.conLog?.(t)
throw e
}
}
@ -126,7 +129,9 @@ class Client extends Connection {
}
onDisconnectRequest (packet) {
console.warn(`Server requested ${packet.hide_disconnect_reason ? 'silent disconnect' : 'disconnect'}: ${packet.message}`)
// TODO: workaround bug in standardjs, waiting for https://github.com/standard/eslint-config-standard/pull/193
const t = `Server requested ${packet.hide_disconnect_reason ? 'silent disconnect' : 'disconnect'}: ${packet.message}`
this.conLog?.(t)
this.emit('kick', packet)
this.close()
}
@ -172,7 +177,7 @@ class Client extends Connection {
return
}
const pakData = { name: des.data.name, params: des.data.params }
this.inLog?.('-> C', pakData.name, this.options.loggging ? serialize(pakData.params) : '')
this.inLog?.('-> C', pakData.name, this.options.logging ? serialize(pakData.params) : '')
this.emit('packet', des)
if (debugging) {

View file

@ -16,7 +16,7 @@ function createClient (options) {
client.ping().then(data => {
const ad = advertisement.fromServerName(data)
client.options.version = options.version ?? (Versions[ad.version] ? ad.version : CURRENT_VERSION)
console.log(`Connecting to server ${ad.motd} (${ad.name}), version ${ad.version}`, client.options.version !== ad.version ? ` (as ${client.options.version})` : undefined)
if (client.conLog) client.conLog(`Connecting to server ${ad.motd} (${ad.name}), version ${ad.version}`, client.options.version !== ad.version ? ` (as ${client.options.version})` : '')
connect(client)
}, client)
}