Better handle ping timeout, update documentation (#218)

* Better handle ping_timeout, update documentation

* Remove extra semicolon

* Change ping_timeout to client.emit('error', e) for more normalized handling

* Remove extra RakTimeout import
This commit is contained in:
Stephen O'Connor 2022-06-09 10:45:58 -07:00 committed by GitHub
commit aacd4b4256
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 11 deletions

View file

@ -37,6 +37,7 @@ The following special events are emitted by the client on top of protocol packet
* 'connect_allowed' - Emitted after the client has pinged the server and gets version information.
* 'heartbeat' - Emitted after two successful tick_sync (keepalive) packets have been sent bidirectionally
* 'packet' - Emitted for all packets received by client
* 'session' - When the client has finished authenticating and connecting
## be.createServer(options) : Server

View file

@ -12,6 +12,7 @@ function createClient (options) {
const client = new Client({ port: 19132, ...options, delayedInit: true })
function onServerInfo () {
client.on('connect_allowed', () => connect(client))
if (options.skipPing) {
client.init()
} else {
@ -20,7 +21,7 @@ function createClient (options) {
client.options.version = options.version ?? (Options.Versions[adVersion] ? adVersion : Options.CURRENT_VERSION)
client.conLog?.(`Connecting to server ${ad.motd} (${ad.name}), version ${ad.version}`, client.options.version !== ad.version ? ` (as ${client.options.version})` : '')
client.init()
})
}).catch(e => client.emit('error', e))
}
}
@ -29,8 +30,6 @@ function createClient (options) {
} else {
onServerInfo()
}
client.on('connect_allowed', () => connect(client))
return client
}
@ -81,9 +80,11 @@ function connect (client) {
async function ping ({ host, port }) {
const con = new RakClient({ host, port })
const ret = await con.ping()
con.close()
return advertisement.fromServerName(ret)
try {
return advertisement.fromServerName(await con.ping())
} finally {
con.close()
}
}
module.exports = { createClient, ping }

View file

@ -4,11 +4,13 @@ const { waitFor } = require('./datatypes/util')
let Client, Server, PacketPriority, EncapsulatedPacket, PacketReliability, Reliability
class RakTimeout extends Error {};
module.exports = nativeRaknet => {
if (nativeRaknet) {
try {
({ Client, Server, PacketPriority, PacketReliability } = require('raknet-native'))
return { RakServer: RakNativeServer, RakClient: RakNativeClient }
return { RakServer: RakNativeServer, RakClient: RakNativeClient, RakTimeout }
} catch (e) {
({ Client, Server, EncapsulatedPacket, Reliability } = require('jsp-raknet'))
console.debug('[raknet] native not found, using js', e)
@ -17,7 +19,7 @@ module.exports = nativeRaknet => {
} else {
({ Client, Server, EncapsulatedPacket, Reliability } = require('jsp-raknet'))
}
return { RakServer: RakJsServer, RakClient: RakJsClient }
return { RakServer: RakJsServer, RakClient: RakJsClient, RakTimeout }
}
class RakNativeClient extends EventEmitter {
@ -54,7 +56,7 @@ class RakNativeClient extends EventEmitter {
done(ret.extra.toString())
}
})
}, timeout, () => { throw new Error('Ping timed out') })
}, timeout, () => { throw new RakTimeout('Ping timed out') })
}
connect () {
@ -193,7 +195,7 @@ class RakJsClient extends EventEmitter {
this.worker.postMessage({ type: 'ping' })
return waitFor(res => {
this.pongCb = data => res(data)
}, timeout, () => { throw new Error('Ping timed out') })
}, timeout, () => { throw new RakTimeout('Ping timed out') })
} else {
if (!this.raknet) this.raknet = new Client(this.options.host, this.options.port)
return waitFor(res => {
@ -201,7 +203,7 @@ class RakJsClient extends EventEmitter {
this.raknet.close()
res(data)
})
}, timeout, () => { throw new Error('Ping timed out') })
}, timeout, () => { throw new RakTimeout('Ping timed out') })
}
}
}