diff --git a/docs/API.md b/docs/API.md index c1f0b7f..6955f91 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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 diff --git a/src/createClient.js b/src/createClient.js index 3c19928..f48b1a3 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -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 } diff --git a/src/rak.js b/src/rak.js index 182ef09..19f313c 100644 --- a/src/rak.js +++ b/src/rak.js @@ -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') }) } } }