API reference / @jsprismarine/raknet / ClientSocket
Class: ClientSocket
Defined in: raknet/src/ClientSocket.ts:115
RakNet from the other side: opens one outgoing connection and drives a RakNetSession over it.
The reliability layer is not reimplemented here. A session does not know or care which kind of peer owns its socket - see RakNetPeer - so ACK/NACK, ordering, fragmentation and retransmission are the exact code paths the server runs, which is the only way an outgoing connection is worth anything as a test of the incoming one.
What is here is the part that has no session to lean on: the offline exchange. None of it is reliable, so this class owns the retry timer and the MTU ladder, and hands over the moment OpenConnectionReply2 arrives.
Example
const client = new ClientSocket(logger);
const session = await client.connect('127.0.0.1', 19132);
client.on('encapsulated', (frame) => handle(frame.content));Extends
EventEmitter
Implements
Constructors
Constructor
new ClientSocket(
logger,options?):ClientSocket
Defined in: raknet/src/ClientSocket.ts:130
Parameters
logger
options?
ClientSocketOptions = {}
Returns
ClientSocket
Overrides
EventEmitter.constructor
Methods
connect()
connect(
address,port):Promise<RakNetSession>
Defined in: raknet/src/ClientSocket.ts:155
Opens a connection and resolves once the handshake has completed.
Rejects with the server's own reason when it refuses us - full, banned, wrong protocol version - rather than letting all four failures look like a timeout.
Parameters
address
string
the server's address.
port
number
the server's port.
Returns
Promise<RakNetSession>
the connected session.
disconnect()
disconnect(
reason?):void
Defined in: raknet/src/ClientSocket.ts:252
Says goodbye properly, so the server drops the session rather than timing it out.
Parameters
reason?
string = 'client disconnect'
Returns
void
getAddress()
getAddress():
InetAddress
Defined in: raknet/src/ClientSocket.ts:246
The local address the socket bound to, available once connect has begun.
Returns
getClientGuid()
getClientGuid():
bigint
Defined in: raknet/src/ClientSocket.ts:237
Our own guid - the one the server knows us by.
Returns
bigint
getLogger()
getLogger():
Logger
Defined in: raknet/src/ClientSocket.ts:241
Returns
Implementation of
getSession()
getSession():
RakNetSession|null
Defined in: raknet/src/ClientSocket.ts:232
The live session, or null before the handshake completes and after it ends.
Returns
RakNetSession | null
kill()
kill():
void
Defined in: raknet/src/ClientSocket.ts:257
Tears everything down. The socket cannot be reused afterwards.
Returns
void
onOfflineFailure()
onOfflineFailure(
reason):void
Defined in: raknet/src/ClientSocket.ts:366
The server refused us, and said why. Terminal: retrying cannot change the answer.
Parameters
reason
string
Returns
void
onOpenConnectionReply1()
onOpenConnectionReply1(
reply):void
Defined in: raknet/src/ClientSocket.ts:325
The server is willing and has told us the MTU it saw. Everything downstream sizes buffers against this, so it is clamped exactly as the server clamps ours.
Parameters
reply
Returns
void
onOpenConnectionReply2()
onOpenConnectionReply2(
reply,rinfo):void
Defined in: raknet/src/ClientSocket.ts:337
The connection is accepted: build the session and open the connected handshake.
Parameters
reply
rinfo
RemoteInfo
Returns
void
onUnconnectedPong()
onUnconnectedPong(
pong,rinfo):void
Defined in: raknet/src/ClientSocket.ts:352
Parameters
pong
rinfo
RemoteInfo
Returns
void
ping()
ping(
address,port,timeoutMs?):Promise<PingResult>
Defined in: raknet/src/ClientSocket.ts:195
Asks a server for its MOTD without connecting. Several may be in flight at once, so a server list can be refreshed in one pass.
Parameters
address
string
the server's address.
port
number
the server's port.
timeoutMs?
number = 2_000
how long to wait for the pong.
Returns
Promise<PingResult>
removeSession()
removeSession(
session,reason?):void
Defined in: raknet/src/ClientSocket.ts:307
The session has finished disconnecting and should be forgotten. Called exactly once per session, from Session.disconnect.
Parameters
session
reason?
string
Returns
void
Implementation of
sendBuffer()
sendBuffer(
buffer,rinfo):void
Defined in: raknet/src/ClientSocket.ts:299
Parameters
buffer
Buffer
rinfo
RemoteInfo
Returns
void
sendPacket()
sendPacket(
packet,rinfo):void
Defined in: raknet/src/ClientSocket.ts:294
Encodes and sends a packet to rinfo. Sessions never touch a socket directly - a closed or unbound one has to be the peer's problem, since only the peer knows.
Parameters
packet
rinfo
RemoteInfo
Returns
void