Documents for @litert/websocket / Decl / IWebSocket
Interface: IWebSocket
Defined in: src/lib/Decl.ts:305
The base interface of websocket connection, implements both readable and writable stream.
Extended by
Properties
connected
readonlyconnected:boolean
Defined in: src/lib/Decl.ts:310
Tell whether the connection is connected.
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.
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.
frameReceiveMode
readonlyframeReceiveMode:EFrameReceiveMode
Defined in: src/lib/Decl.ts:317
The mode of receiving frames.
Default
EFrameReceiveMode.STANDARDisServer
readonlyisServer:boolean
Defined in: src/lib/Decl.ts:367
Tell if this socket is a server-side socket.
localAddress
readonlylocalAddress:string|null
Defined in: src/lib/Decl.ts:347
Tell the local IP address of this socket.
localPort
readonlylocalPort:number|null
Defined in: src/lib/Decl.ts:352
Tell the local port of this socket.
maxMessageSize
readonlymaxMessageSize:number
Defined in: src/lib/Decl.ts:374
The maximum size of each message body.
Default
67108864 (64 MiB)peerCertificate
readonlypeerCertificate:PeerCertificate|null
Defined in: src/lib/Decl.ts:357
Tell the information of TLS certificate of remote peer.
remoteAddress
readonlyremoteAddress:string|null
Defined in: src/lib/Decl.ts:337
Tell the remote IP address of this socket.
remotePort
readonlyremotePort:number|null
Defined in: src/lib/Decl.ts:342
Tell the remote port of this socket.
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
60000tls
readonlytls:boolean
Defined in: src/lib/Decl.ts:362
Tell if this socket is a TLS socket.
writable
readonlywritable:boolean
Defined in: src/lib/Decl.ts:322
Tell whether the connection is writable.
Methods
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
destroy()
destroy():
void
Defined in: src/lib/Decl.ts:510
Close the socket and disable all ops on this socket.
Returns
void
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.
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
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
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
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
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
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
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
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).
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).
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).
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).