NodeWebSocket class

An implementation of ISocket to use with a NodeWebSocketFactory to create a WebSocket server.

Constructors

NodeWebSocket(WebSocket)

Creates a new NodeWebSocket instance.

Properties

isConnected

Indicates if the 'ws' WebSocket is currently connected and ready to send messages.

Methods

close(number, string)

Close the socket.

connect(string, number)

Connects to the supporting socket using WebSocket protocol.

create(INodeIncomingMessage, INodeSocket, INodeBuffer)

Create and set a ws WebSocket with an HTTP Request, Socket and Buffer.

setOnCloseHandler((x: any) => void)

Set the callback to call when encountering socket closures.

setOnErrorHandler((x: any) => void)

Set the callback to call when encountering errors.

setOnMessageHandler((x: any) => void)

Set the handler for 'data' and 'message' events received on the socket.

write(INodeBuffer)

Writes a buffer to the socket and sends it.

Constructor Details

NodeWebSocket(WebSocket)

Creates a new NodeWebSocket instance.

new NodeWebSocket(wsSocket?: WebSocket)

Parameters

wsSocket

WebSocket

The ws WebSocket instance to build this connection on.

Property Details

isConnected

Indicates if the 'ws' WebSocket is currently connected and ready to send messages.

boolean isConnected

Property Value

boolean

true if the underlying websocket is ready and availble to send messages, otherwise false.

Method Details

close(number, string)

Close the socket.

function close(code?: number, data?: string)

Parameters

code

number

Optional status code to explain why the connection has closed.

data

string

Optional additional data to explain why the connection has closed.

Remarks

Optionally pass in a status code and string explaining why the connection is closing.

connect(string, number)

Connects to the supporting socket using WebSocket protocol.

function connect(serverAddress: string, port?: number): Promise<void>

Parameters

serverAddress

string

The address the server is listening on.

port

number

The port the server is listening on, defaults to 8082.

Returns

Promise<void>

A Promise that resolves when the websocket connection is closed, or rejects on an error.

create(INodeIncomingMessage, INodeSocket, INodeBuffer)

Create and set a ws WebSocket with an HTTP Request, Socket and Buffer.

function create(req: INodeIncomingMessage, socket: INodeSocket, head: INodeBuffer): Promise<void>

Parameters

req
INodeIncomingMessage

An HTTP Request matching the INodeIncomingMessage interface.

socket
INodeSocket

A Socket INodeSocket interface.

head
INodeBuffer

A Buffer INodeBuffer interface.

Returns

Promise<void>

A Promise that resolves after the WebSocket upgrade has been handled, otherwise rejects with a thrown error.

setOnCloseHandler((x: any) => void)

Set the callback to call when encountering socket closures.

function setOnCloseHandler(handler: (x: any) => void)

Parameters

handler

(x: any) => void

The callback to handle the "close" event.

setOnErrorHandler((x: any) => void)

Set the callback to call when encountering errors.

function setOnErrorHandler(handler: (x: any) => void)

Parameters

handler

(x: any) => void

The callback to handle the "error" event.

setOnMessageHandler((x: any) => void)

Set the handler for 'data' and 'message' events received on the socket.

function setOnMessageHandler(handler: (x: any) => void)

Parameters

handler

(x: any) => void

The callback to handle the "message" event.

write(INodeBuffer)

Writes a buffer to the socket and sends it.

function write(buffer: INodeBuffer)

Parameters

buffer
INodeBuffer

The buffer of data to send across the connection.