Skip to content

Documents for @litert/websocket / Decl / IClient

Interface: IClient

Defined in: src/lib/Decl.ts:105

The interface of websocket client.

Extends

Properties

connected

readonly connected: boolean

Defined in: src/lib/Decl.ts:310

Tell whether the connection is connected.

Inherited from

IWebSocket.connected


ended

readonly ended: boolean

Defined in: src/lib/Decl.ts:332

Tell whether the connection is ended reading, and no more data could be read from.

Inherited from

IWebSocket.ended


finished

readonly finished: boolean

Defined in: src/lib/Decl.ts:327

Tell whether the connection is finished writing, and no more data could be written in.

Inherited from

IWebSocket.finished


frameReceiveMode

readonly frameReceiveMode: EFrameReceiveMode

Defined in: src/lib/Decl.ts:317

The mode of receiving frames.

Default

ts
EFrameReceiveMode.STANDARD

Inherited from

IWebSocket.frameReceiveMode


isServer

readonly isServer: boolean

Defined in: src/lib/Decl.ts:367

Tell if this socket is a server-side socket.

Inherited from

IWebSocket.isServer


localAddress

readonly localAddress: string | null

Defined in: src/lib/Decl.ts:347

Tell the local IP address of this socket.

Inherited from

IWebSocket.localAddress


localPort

readonly localPort: number | null

Defined in: src/lib/Decl.ts:352

Tell the local port of this socket.

Inherited from

IWebSocket.localPort


maxMessageSize

readonly maxMessageSize: number

Defined in: src/lib/Decl.ts:374

The maximum size of each message body.

Default

ts
67108864 (64 MiB)

Inherited from

IWebSocket.maxMessageSize


peerCertificate

readonly peerCertificate: PeerCertificate | null

Defined in: src/lib/Decl.ts:357

Tell the information of TLS certificate of remote peer.

Inherited from

IWebSocket.peerCertificate


remoteAddress

readonly remoteAddress: string | null

Defined in: src/lib/Decl.ts:337

Tell the remote IP address of this socket.

Inherited from

IWebSocket.remoteAddress


remotePort

readonly remotePort: number | null

Defined in: src/lib/Decl.ts:342

Tell the remote port of this socket.

Inherited from

IWebSocket.remotePort


timeout

timeout: number

Defined in: src/lib/Decl.ts:386

The timeout in milliseconds for the connections after connection is established.

Timeout means the connection is idle for a long time, and the connection will be closed.

Set to 0 to disable the timeout.

Default

ts
60000

Inherited from

IWebSocket.timeout


tls

readonly tls: boolean

Defined in: src/lib/Decl.ts:362

Tell if this socket is a TLS socket.

Inherited from

IWebSocket.tls


writable

readonly writable: boolean

Defined in: src/lib/Decl.ts:322

Tell whether the connection is writable.

Inherited from

IWebSocket.writable

Methods

connect()

connect(): Promise<void>

Defined in: src/lib/Decl.ts:140

Connect to the server.

This method can only be called once, and only when the connection is not established yet. Considering the early data sent by server during the handshake, you must set up the message event handler before calling this method. Or you will miss the early data sent by server.

Returns

Promise<void>

The promise for connecting, which will be resolved when connection is established, or rejected if any error happened.

Throws

E_HANDSHAKE_FAILED will be thrown if the connection is failed.

Throws

E_TIMEOUT will be thrown if timeout happened during connecting.


createMessageWriter()

createMessageWriter(opcode, opts?): IMessageWriter

Defined in: src/lib/Decl.ts:455

Create a fragment writer for a message.

NOTICES:

  • Don't forget to call end() method of the writer to send out the last frame of the message.
  • Don't send any other messages out of the writer before calling its end() method.

Parameters

opcode

EOpcode

opts?

WritableOptions<Writable>

Returns

IMessageWriter

Inherited from

IWebSocket.createMessageWriter


destroy()

destroy(): void

Defined in: src/lib/Decl.ts:510

Close the socket and disable all ops on this socket.

Returns

void

Inherited from

IWebSocket.destroy


end()

end(reason?, callback?): boolean

Defined in: src/lib/Decl.ts:505

Send a single CLOSE message to remote-side, and then close the socket.

If socket is already closed, nothing will happen, and false will be returned.

Parameters

reason?

ECloseReason

The reason code for closing. [default: ECloseReason.BYE]

callback?

IErrorCallback

Returns

boolean

true if the data is flushed to kernel buffer completely, otherwise false.

Inherited from

IWebSocket.end


on()

Call Signature

on(event, listener): this

Defined in: src/lib/Decl.ts:399

Register a callback for event "message", which will be triggered when a new message is received.

The type msg parameter in listener depends on the frameReceiveMode property.

Parameters
event

"message"

The event name.

listener

(msg) => void

The callback function.

Returns

this

See

IWebSocket.frameReceiveMode

Inherited from

IWebSocket.on

Call Signature

on(event, listener): this

Defined in: src/lib/Decl.ts:406

Register a callback for event "error", which will be triggered when an error occurred.

Parameters
event

"error"

The event name.

listener

(error) => void

The callback function.

Returns

this

Inherited from

IWebSocket.on

Call Signature

on(event, listener): this

Defined in: src/lib/Decl.ts:413

Register a callback for event "drain", which will be triggered when an all data in buffer flushed out.

Parameters
event

"drain"

The event name.

listener

() => void

The callback function.

Returns

this

Inherited from

IWebSocket.on

Call Signature

on(event, listener): this

Defined in: src/lib/Decl.ts:421

Register a callback for event "end", which will be triggered when the websocket is closed by remote-side.

Parameters
event

"end"

The event name.

listener

() => void

The callback function.

Returns

this

Inherited from

IWebSocket.on

Call Signature

on(event, listener): this

Defined in: src/lib/Decl.ts:429

Register a callback for event "finish", which will be triggered when the websocket is closed by local-side.

Parameters
event

"finish"

The event name.

listener

() => void

The callback function.

Returns

this

Inherited from

IWebSocket.on

Call Signature

on(event, listener): this

Defined in: src/lib/Decl.ts:437

Register a callback for event "timeout", which will be triggered when the websocket is closed by local-side.

Parameters
event

"timeout"

The event name.

listener

() => void

The callback function.

Returns

this

Inherited from

IWebSocket.on

Call Signature

on(event, listener): this

Defined in: src/lib/Decl.ts:444

Register a callback for event "close", which will be triggered when the websocket is closed by local-side.

Parameters
event

"close"

The event name.

listener

(error?) => void

The callback function.

Returns

this

Inherited from

IWebSocket.on


ping()

ping(data?, callback?): boolean

Defined in: src/lib/Decl.ts:485

Send a single PING message to remote-side, with an optional data.

Parameters

data?

string | Buffer<ArrayBufferLike> | (string | Buffer<ArrayBufferLike>)[]

callback?

IErrorCallback

Returns

boolean

true if the data is flushed to kernel buffer completely, otherwise false.

Throws

E_CONN_LOST will be thrown if the connection is closed.

Throws

E_CONN_READONLY will be thrown if the connection is not writable (half-closed).

Inherited from

IWebSocket.ping


pong()

pong(data?, callback?): boolean

Defined in: src/lib/Decl.ts:495

Send a single PONG message to remote-side, with an optional data.

Parameters

data?

string | Buffer<ArrayBufferLike> | (string | Buffer<ArrayBufferLike>)[]

callback?

IErrorCallback

Returns

boolean

true if the data is flushed to kernel buffer completely, otherwise false.

Throws

E_CONN_LOST will be thrown if the connection is closed.

Throws

E_CONN_READONLY will be thrown if the connection is not writable (half-closed).

Inherited from

IWebSocket.pong


setMasking()

setMasking(key): void

Defined in: src/lib/Decl.ts:123

Set the mask key for websocket frames.

  • Set to false to disable masking, which is not recommended for compatibility.
  • Set to true to generate a random mask key for every frame.
  • Set to a Buffer object to use it as mask key for every frame.

The RFC-6455 requires masking all frames from client, but not from server. This library will mask all frames by default, for compatibility, but not enforced. The server can still accept unmasked frames from client. If you want to disable masking, you should make sure the server will not refuse the unmasked frames.

NOTE: The default behavior is to mask all frames using an initial random key.

Parameters

key

The key for websocket handshake.

boolean | Buffer<ArrayBufferLike>

Returns

void


writeBinary()

writeBinary(data, callback?): boolean

Defined in: src/lib/Decl.ts:475

Send a binary message to remote-side, in a single BINARY message.

Parameters

data

string | Buffer<ArrayBufferLike> | (string | Buffer<ArrayBufferLike>)[]

callback?

IErrorCallback

Returns

boolean

true if the data is flushed to kernel buffer completely, otherwise false.

Throws

E_CONN_LOST will be thrown if the connection is closed.

Throws

E_CONN_READONLY will be thrown if the connection is not writable (half-closed).

Inherited from

IWebSocket.writeBinary


writeText()

writeText(data, callback?): boolean

Defined in: src/lib/Decl.ts:465

Send a text message to remote-side, in a single TEXT message.

Parameters

data

string | Buffer<ArrayBufferLike> | (string | Buffer<ArrayBufferLike>)[]

callback?

IErrorCallback

Returns

boolean

true if the data is flushed to kernel buffer completely, otherwise false.

Throws

E_CONN_LOST will be thrown if the connection is closed.

Throws

E_CONN_READONLY will be thrown if the connection is not writable (half-closed).

Inherited from

IWebSocket.writeText