-
Notifications
You must be signed in to change notification settings - Fork 2
Client
Class for wrapping a socket (TCP, Websocket or Virtual) under a common interface.
Socket specific functions need to be overridden/implemented in dervived classes.
• new Client(clientOptions)
| Name | Type |
|---|---|
clientOptions |
ClientOptions |
• Optional clientOptions: ClientOptions
• eventHandlers: Object
▪ [key: string]: [Function[], (Buffer | undefined)[]]
• isClosed: boolean
▸ close(): void
Close socket.
void
▸ connect(): void
Connect to server.
void
▸ getLocalAddress(): undefined | string
undefined | string
▸ getLocalPort(): undefined | number
undefined | number
▸ getRemoteAddress(): undefined | string
undefined | string
▸ getRemotePort(): undefined | number
undefined | number
▸ Private off(event, fn): void
Base off event procedure responsible for removing a callback from the list of event handlers.
| Name | Type | Description |
|---|---|---|
event |
string |
event name. |
fn |
Function |
callback. |
void
▸ offClose(fn): void
Unhook handler for close event.
| Name | Type | Description |
|---|---|---|
fn |
Function |
remove close callback. |
void
▸ offConnect(fn): void
Unhook handler for connection event.
| Name | Type | Description |
|---|---|---|
fn |
Function |
remove connect callback. |
void
▸ offData(fn): void
Unhook handler for incoming data.
| Name | Type | Description |
|---|---|---|
fn |
Function |
remove data callback. |
void
▸ offError(fn): void
Unhook handler for socket errors.
| Name | Type | Description |
|---|---|---|
fn |
Function |
remove existing error callback |
void
▸ Private on(event, fn): void
Base on event procedure responsible for adding a callback to the list of event handlers.
| Name | Type | Description |
|---|---|---|
event |
string |
event name. |
fn |
Function |
callback. |
void
▸ onClose(fn): void
User hook for close event.
| Name | Type | Description |
|---|---|---|
fn |
Function |
on close callback. |
void
▸ onConnect(fn): void
User hook for connection event.
| Name | Type | Description |
|---|---|---|
fn |
Function |
on connect callback. |
void
▸ onData(fn): void
User hook for incoming data.
| Name | Type | Description |
|---|---|---|
fn |
Function |
on data callback. Function is passed a Buffer object. |
void
▸ onError(fn): void
User hook for socket errors.
| Name | Type | Description |
|---|---|---|
fn |
Function |
on error callback. Function is passed a Buffer object with the error message |
void
▸ send(data): void
Send buffer on socket.
throws An error will be thrown when buffer data type is incompatible.
| Name | Type | Description |
|---|---|---|
data |
Buffer |
to be sent |
void
▸ sendString(data): void
Send string on socket.
| Name | Type |
|---|---|
data |
string |
void
▸ Protected socketClose(): void
Socket-specific close procedure.
void
▸ Protected socketClosed(hadError): void
Base close event procedure responsible for triggering the close event.
| Name | Type |
|---|---|
hadError |
boolean |
void
▸ Protected socketConnect(): void
Create the socket object and initiate a connection. This only done for initiating client sockets. A server listener socket client is already connected and must be passed in the constructor.
void
▸ Protected socketConnected(): void
Base connect event procedure responsible for triggering the connect event.
void
▸ Protected socketData(data): void
Base data event procedure responsible for triggering the data event.
| Name | Type | Description |
|---|---|---|
data |
Buffer |
data buffer. |
void
▸ Protected socketError(message): void
Base error event procedure responsible for triggering the error event.
| Name | Type |
|---|---|
message |
Buffer |
void
▸ Protected socketHook(): void
Hook events on the socket.
void
▸ Protected socketSend(buffer): void
Send the given buffer on socket. Socket specific implementation.
| Name | Type |
|---|---|
buffer |
Buffer |
void
▸ Private triggerEvent(event, data?, doBuffer?): void
Trigger event calls the appropriate handler based on the event name.
| Name | Type | Default value | Description |
|---|---|---|---|
event |
string |
undefined |
event name. |
data? |
any |
undefined |
- |
doBuffer |
boolean |
false |
- |
void