Documents for @litert/websocket / Decl / IClient
Interface: IClient
Defined in: src/lib/Decl.ts:105
The interface of websocket client.
Extends
Properties
connected
readonlyconnected:boolean
Defined in: src/lib/Decl.ts:310
Tell whether the connection is connected.
Inherited from
ended
readonlyended: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
finished
readonlyfinished: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
frameReceiveMode
readonlyframeReceiveMode:EFrameReceiveMode
Defined in: src/lib/Decl.ts:317
The mode of receiving frames.
Default
EFrameReceiveMode.STANDARDInherited from
isServer
readonlyisServer:boolean
Defined in: src/lib/Decl.ts:367
Tell if this socket is a server-side socket.
Inherited from
localAddress
readonlylocalAddress:string|null
Defined in: src/lib/Decl.ts:347
Tell the local IP address of this socket.
Inherited from
localPort
readonlylocalPort:number|null
Defined in: src/lib/Decl.ts:352
Tell the local port of this socket.
Inherited from
maxMessageSize
readonlymaxMessageSize:number
Defined in: src/lib/Decl.ts:374
The maximum size of each message body.
Default
67108864 (64 MiB)Inherited from
peerCertificate
readonlypeerCertificate:PeerCertificate|null
Defined in: src/lib/Decl.ts:357
Tell the information of TLS certificate of remote peer.
Inherited from
remoteAddress
readonlyremoteAddress:string|null
Defined in: src/lib/Decl.ts:337
Tell the remote IP address of this socket.
Inherited from
remotePort
readonlyremotePort:number|null
Defined in: src/lib/Decl.ts:342
Tell the remote port of this socket.
Inherited from
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
0to disable the timeout.
Default
60000Inherited from
tls
readonlytls:boolean
Defined in: src/lib/Decl.ts:362
Tell if this socket is a TLS socket.
Inherited from
writable
readonlywritable:boolean
Defined in: src/lib/Decl.ts:322
Tell whether the connection is writable.
Inherited from
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
messageevent 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
opts?
WritableOptions<Writable>
Returns
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
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
falsewill be returned.
Parameters
reason?
The reason code for closing. [default: ECloseReason.BYE]
callback?
Returns
boolean
true if the data is flushed to kernel buffer completely, otherwise false.
Inherited from
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
msgparameter inlistenerdepends on theframeReceiveModeproperty.
Parameters
event
"message"
The event name.
listener
(msg) => void
The callback function.
Returns
this
See
IWebSocket.frameReceiveMode
Inherited from
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
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
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
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
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
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
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?
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
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?
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
setMasking()
setMasking(
key):void
Defined in: src/lib/Decl.ts:123
Set the mask key for websocket frames.
- Set to
falseto disable masking, which is not recommended for compatibility. - Set to
trueto generate a random mask key for every frame. - Set to a
Bufferobject 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?
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
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?
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).