Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ServerWebSocket

Represents a server side WebSocket.

Instances of this class are passed into a {@link HttpServer#webSocketHandler} or provided when a WebSocket handshake is manually {@link HttpServerRequest#toWebSocket}ed.

Hierarchy

  • ServerWebSocket

Implements

Index

Methods

accept

  • accept(): void
  • Accept the WebSocket and terminate the WebSocket handshake.

    This method should be called from the WebSocket handler to explicitly accept the WebSocket and terminate the WebSocket handshake.

    Returns void

binaryHandlerID

  • binaryHandlerID(): string
  • When a WebSocket is created, it may register an event handler with the event bus - the ID of that handler is given by this method.

    By default, no handler is registered, the feature must be enabled via WebSocketConnectOptions or HttpServerOptions.

    Given this ID, a different event loop can send a binary frame to that event handler using the event bus and that buffer will be received by this instance in its own event loop and written to the underlying connection. This allows you to write data to other WebSockets which are owned by different event loops.

    Returns string

binaryMessageHandler

  • Set a binary message handler on the connection. This handler serves a similar purpose to {@link ServerWebSocket#handler} except that if a message comes into the socket in multiple frames, the data from the frames will be aggregated into a single buffer before calling the handler (using {@link WebSocketFrame#isFinal} to find the boundaries).

    Parameters

    Returns WebSocketBase

close

  • close(): PromiseLike<void>
  • close(handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): void
  • close(statusCode: number): PromiseLike<void>
  • close(statusCode: number, handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): void
  • close(statusCode: number, reason: string | null | undefined): PromiseLike<void>
  • close(statusCode: number, reason: string | null | undefined, handler: ((res: AsyncResult<void>) => void) | Handler<AsyncResult<void>>): void
  • Same as {@link ServerWebSocket#close} but with an handler called when the operation completes

    Returns PromiseLike<void>

  • Same as {@link ServerWebSocket#close} but with an handler called when the operation completes

    Parameters

    Returns void

  • Same as {@link WebSocketBase#close} but with an handler called when the operation completes

    Parameters

    • statusCode: number

    Returns PromiseLike<void>

  • Same as {@link WebSocketBase#close} but with an handler called when the operation completes

    Parameters

    Returns void

  • Same as {@link WebSocketBase#close} but with an handler called when the operation completes

    Parameters

    • statusCode: number
    • reason: string | null | undefined

    Returns PromiseLike<void>

  • Same as {@link WebSocketBase#close} but with an handler called when the operation completes

    Parameters

    Returns void

closeHandler

  • Parameters

    • handler: ((res: void) => void) | Handler<void> | null | undefined

    Returns ServerWebSocket

closeReason

  • closeReason(): string
  • Returns the close reason message from the remote endpoint or null when not yet received.

    Returns string

closeStatusCode

  • closeStatusCode(): number
  • Returns the close status code received from the remote endpoint or null when not yet received.

    Returns number

drainHandler

  • Parameters

    • handler: ((res: void) => void) | Handler<void> | null | undefined

    Returns ServerWebSocket

end

  • Same as but with an handler called when the operation completes

    Parameters

    Returns PromiseLike<void>

  • Same as but with an handler called when the operation completes

    Parameters

    Returns void

  • Calls {@link WebSocketBase#close}

    Returns PromiseLike<void>

  • Calls {@link WebSocketBase#close}

    Parameters

    Returns void

endHandler

  • Parameters

    • endHandler: ((res: void) => void) | Handler<void> | null | undefined

    Returns ServerWebSocket

exceptionHandler

fetch

frameHandler

handler

  • Parameters

    Returns ServerWebSocket

headers

  • Returns the HTTP headers when the WebSocket is first obtained in the handler.

    The headers will be null on subsequent interactions.

    Returns MultiMap

host

  • host(): string | null
  • Returns string | null

isClosed

  • isClosed(): boolean
  • Returns boolean

isSsl

  • isSsl(): boolean
  • Returns boolean

localAddress

path

  • path(): string
  • Returns string

pause

pipe

  • Pause this stream and return a to transfer the elements of this stream to a destination .

    The stream will be resumed when the pipe will be wired to a WriteStream.

    Returns Pipe<Buffer>

pipeTo

  • Pipe this ReadStream to the WriteStream.

    Elements emitted by this stream will be written to the write stream until this stream ends or fails.

    Once this stream has ended or failed, the write stream will be ended and the handler will be called with the result.

    Parameters

    Returns PromiseLike<void>

  • Pipe this ReadStream to the WriteStream.

    Elements emitted by this stream will be written to the write stream until this stream ends or fails.

    Once this stream has ended or failed, the write stream will be ended and the handler will be called with the result.

    Parameters

    Returns void

pongHandler

  • Set a pong frame handler on the connection. This handler will be invoked every time a pong frame is received on the server, and can be used by both clients and servers since the RFC 6455 section 5.5.2 and section 5.5.3 do not specify whether the client or server sends a ping.

    Pong frames may be at most 125 bytes (octets).

    There is no ping handler since ping frames should immediately be responded to with a pong frame with identical content

    Pong frames may be received unsolicited.

    Parameters

    Returns WebSocketBase

query

  • query(): string | null
  • Returns string | null

reject

  • reject(): void
  • reject(status: number): void
  • Reject the WebSocket.

    Calling this method from the WebSocket handler when it is first passed to you gives you the opportunity to reject the WebSocket, which will cause the WebSocket handshake to fail by returning a response code.

    You might use this method, if for example you only want to accept WebSockets with a particular path.

    Returns void

  • Like {@link ServerWebSocket#reject} but with a status.

    Parameters

    • status: number

    Returns void

remoteAddress

resume

scheme

  • scheme(): string | null
  • Returns string | null

setHandshake

  • setHandshake(future: PromiseLike<number>): PromiseLike<number>
  • setHandshake(future: PromiseLike<number>, handler: ((res: AsyncResult<number>) => void) | Handler<AsyncResult<number>>): void
  • Set an asynchronous result for the handshake, upon completion of the specified future, the WebSocket will either be

    • accepted when the future succeeds with the HTTP status code
    • rejected when the future is succeeds with an HTTP status code different than
    • rejected when the future fails with the HTTP status code 500

    The provided future might be completed by the WebSocket itself, e.g calling the {@link ServerWebSocket#close} method will try to accept the handshake and close the WebSocket afterward. Thus it is advised to try to complete the future with or .

    This method should be called from the WebSocket handler to explicitly set an asynchronous handshake.

    Calling this method will override the future completion handler.

    Parameters

    • future: PromiseLike<number>

    Returns PromiseLike<number>

  • Set an asynchronous result for the handshake, upon completion of the specified future, the WebSocket will either be

    • accepted when the future succeeds with the HTTP status code
    • rejected when the future is succeeds with an HTTP status code different than
    • rejected when the future fails with the HTTP status code 500

    The provided future might be completed by the WebSocket itself, e.g calling the {@link ServerWebSocket#close} method will try to accept the handshake and close the WebSocket afterward. Thus it is advised to try to complete the future with or .

    This method should be called from the WebSocket handler to explicitly set an asynchronous handshake.

    Calling this method will override the future completion handler.

    Parameters

    Returns void

setWriteQueueMaxSize

sslSession

  • sslSession(): any
  • Returns any

subProtocol

  • subProtocol(): string
  • Returns the WebSocket sub protocol selected by the WebSocket handshake.

    On the server, the value will be null when the handler receives the WebSocket callback as the handshake will not be completed yet.

    Returns string

textHandlerID

  • textHandlerID(): string
  • When a WebSocket is created, it may register an event handler with the eventbus, the ID of that handler is given by textHandlerID.

    By default, no handler is registered, the feature must be enabled via WebSocketConnectOptions or HttpServerOptions.

    Given this ID, a different event loop can send a text frame to that event handler using the event bus and that buffer will be received by this instance in its own event loop and written to the underlying connection. This allows you to write data to other WebSockets which are owned by different event loops.

    Returns string

textMessageHandler

  • textMessageHandler(handler: ((res: string) => void) | Handler<string> | null | undefined): WebSocketBase
  • Set a text message handler on the connection. This handler will be called similar to the {@link WebSocketBase#binaryMessageHandler}, but the buffer will be converted to a String first

    Parameters

    • handler: ((res: string) => void) | Handler<string> | null | undefined

    Returns WebSocketBase

uri

  • uri(): string
  • Returns string

write

  • Same as but with an handler called when the operation completes

    Parameters

    Returns PromiseLike<void>

  • Same as but with an handler called when the operation completes

    Parameters

    Returns void

writeBinaryMessage

writeFinalBinaryFrame

writeFinalTextFrame

writeFrame

writePing

  • Writes a ping frame to the connection. This will be written in a single frame. Ping frames may be at most 125 bytes (octets).

    This method should not be used to write application data and should only be used for implementing a keep alive or to ensure the client is still responsive, see RFC 6455 Section section 5.5.2.

    There is no handler for ping frames because RFC 6455 clearly states that the only response to a ping frame is a pong frame with identical contents.

    Parameters

    Returns PromiseLike<void>

  • Writes a ping frame to the connection. This will be written in a single frame. Ping frames may be at most 125 bytes (octets).

    This method should not be used to write application data and should only be used for implementing a keep alive or to ensure the client is still responsive, see RFC 6455 Section section 5.5.2.

    There is no handler for ping frames because RFC 6455 clearly states that the only response to a ping frame is a pong frame with identical contents.

    Parameters

    Returns WebSocketBase

writePong

  • Writes a pong frame to the connection. This will be written in a single frame. Pong frames may be at most 125 bytes (octets).

    This method should not be used to write application data and should only be used for implementing a keep alive or to ensure the client is still responsive, see RFC 6455 section 5.5.2.

    There is no need to manually write a pong frame, as the server and client both handle responding to a ping from with a pong from automatically and this is exposed to users. RFC 6455 section 5.5.3 states that pongs may be sent unsolicited in order to implement a one way heartbeat.

    Parameters

    Returns PromiseLike<void>

  • Writes a pong frame to the connection. This will be written in a single frame. Pong frames may be at most 125 bytes (octets).

    This method should not be used to write application data and should only be used for implementing a keep alive or to ensure the client is still responsive, see RFC 6455 section 5.5.2.

    There is no need to manually write a pong frame, as the server and client both handle responding to a ping from with a pong from automatically and this is exposed to users. RFC 6455 section 5.5.3 states that pongs may be sent unsolicited in order to implement a one way heartbeat.

    Parameters

    Returns WebSocketBase

writeQueueFull

  • writeQueueFull(): boolean
  • This will return true if there are more bytes in the write queue than the value set using {@link ServerWebSocket#setWriteQueueMaxSize}

    Returns boolean

writeTextMessage

Generated using TypeDoc