bare-ws
WebSocket library for JavaScript
v3.2.0bare-ws — WebSocket library for JavaScript.
npm i bare-wsUsage
const ws = require('bare-ws')
const server = new ws.Server({ port: 8080 }, (socket) => {
socket.on('data', (data) => {
console.log(data.toString())
})
})
server.on('listening', () => {
const socket = new ws.Socket({ port: 8080 })
socket.write('Hello WebSocket')
})API
Socket
new Socket(opts: WebSocketOptions)
Open a WebSocket connection to url, or wrap an already-connected opts.socket when acting as the server side of a handshake.
Overloads:
new Socket(opts: WebSocketOptions)
new Socket(url: URL | string, opts?: WebSocketOptions)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts | WebSocketOptions | — | Connection options: host/hostname, port, path, secure, or an already-connected socket to wrap. |
_destroy(err: Error | null, cb: StreamCallback): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
err | Error | null | — | — |
cb | StreamCallback | — | — |
_final(cb: StreamCallback): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
cb | StreamCallback | — | — |
_open(cb: StreamCallback): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
cb | StreamCallback | — | — |
_predestroy(): void
_read(size: number): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
size | number | — | — |
_write(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | — | — |
encoding | StreamEncoding | — | — |
cb | StreamCallback | — | — |
_writev(batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
batch | { chunk: unknown; encoding: StreamEncoding }[] | — | — |
cb | StreamCallback | — | — |
close(code?: number, reason?: string | Buffer): void
Start the closing handshake with a status and optional reason. The status
must be valid to send in a close frame, and the UTF-8 encoded reason must
be at most 123 bytes. Defaults to status 1000 with an empty reason.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
code? | number | — | The close status sent to the peer. |
reason? | string | Buffer | — | A human-readable reason, as text or bytes, sent after the status. |
Throws
RangeError—codeis not a valid close status, orreasonis longer than 123 bytes once encoded as UTF-8.
closeCode: number
The status the peer closed with, once it has sent a close frame. 1005 if
the close frame carried no status, and 1006 until one arrives at all, so
a connection that went away without closing keeps it. Readable from a
close listener.
closed: boolean
closeReason: Buffer
The reason the peer closed with, empty unless its close frame carried one
past the status. Readable from a close listener.
cork(): void
destroy(err?: Error | null): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
err? | Error | null | — | — |
destroyed: boolean
destroying: boolean
end(cb?: StreamCallback): this
Overloads:
end(cb?: StreamCallback): this
end(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): this
end(data: unknown, cb?: StreamCallback): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
cb? | StreamCallback | — | — |
errored: Error | null
pause(): this
ping(data?: string | Buffer): void
Send a ping frame to the peer.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data? | string | Buffer | — | The payload of the ping frame; a string is converted to a Buffer. |
Throws
NOT_CONNECTED— the socket has not finished connecting, has closed, or has been ended, since nothing may follow the close frame that ending sends.INVALID_CONTROL_PAYLOAD_LENGTH—datais longer than 125 bytes.TypeError—datais neither a string nor a view of bytes.
pipe<S extends Writable>(dest: S, cb?: StreamCallback): S
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
dest | S | — | — |
cb? | StreamCallback | — | — |
pong(data?: string | Buffer): void
Send a pong frame to the peer.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data? | string | Buffer | — | The payload of the pong frame; a string is converted to a Buffer. |
Throws
NOT_CONNECTED— the socket has not finished connecting, has closed, or has been ended, since nothing may follow the close frame that ending sends.INVALID_CONTROL_PAYLOAD_LENGTH—datais longer than 125 bytes.TypeError—datais neither a string nor a view of bytes.
protocol: string | null
The subprotocol selected by the server. null before the client connection
opens and when the handshake selects no subprotocol.
push(data: unknown | null, encoding?: BufferEncoding): boolean
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | null | — | — |
encoding? | BufferEncoding | — | — |
read(): unknown | null
readable: boolean
resume(): this
setEncoding(encoding: BufferEncoding): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
encoding | BufferEncoding | — | — |
Socket.handshake(req: HTTPClientRequest, cb: (error: WebSocketError | null) => void): void
Perform the client side of the WebSocket opening handshake over req, calling cb with a WebSocketError if the server's response is invalid.
Overloads:
Socket.handshake(req: HTTPClientRequest, cb: (error: WebSocketError | null) => void): void
Socket.handshake(req: HTTPClientRequest, opts: WebSocketHandshakeOptions, cb: (error: WebSocketError | null) => void): voidParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
req | HTTPClientRequest | — | The outgoing HTTP request to upgrade; the WebSocket handshake headers are added to it. |
cb | (error: WebSocketError | null) => void | — | Called once the server's response is validated, with a WebSocketError on failure, otherwise null. |
Socket.WebSocketEvents
interface WebSocketEvents {
message: [payload: Buffer, binary: boolean]
ping: [payload: Buffer]
pong: [payload: Buffer]
data: [data: unknown]
end: []
readable: []
piping: [dest: Writable]
close: []
error: [err: Error]
drain: []
finish: []
pipe: [src: Readable]
}Socket.WebSocketHandshakeOptions
interface WebSocketHandshakeOptions {
timeout?: number
}Socket.WebSocketOptions
interface WebSocketOptions {
headers?: Record<string, HTTPHeaderValue>
host?: string
hostname?: string
path?: string
port?: string | number
secure?: boolean
socket?: TCPSocket
isServer?: boolean
maxPayload?: number
maxFragments?: number
handshakeTimeout?: number
idleTimeout?: number
closeTimeout?: number
}host/hostname (an alias for host, for Node.js compatibility), port, and path address the connection. secure uses TLS (wss:) for the underlying connection when true. socket is an already-connected TCP socket to use instead of opening a new connection.
uncork(): void
unshift(data: unknown | null, encoding?: BufferEncoding): boolean
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | null | — | — |
encoding? | BufferEncoding | — | — |
writable: boolean
write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
Overloads:
write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
write(data: unknown, cb?: StreamCallback): booleanParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | unknown | — | — |
encoding? | BufferEncoding | — | — |
cb? | StreamCallback | — | — |
Server
new Server(onconnection: (socket: WebSocket, req: HTTPIncomingMessage) => void)
Create a server, optionally backed by opts.server, and emit connection for each successful WebSocket upgrade.
Overloads:
new Server(onconnection: (socket: WebSocket, req: HTTPIncomingMessage) => void)
new Server(opts?: WebSocketServerOptions, onconnection?: (socket: WebSocket, req: HTTPIncomingMessage) => void)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
onconnection | (socket: WebSocket, req: HTTPIncomingMessage) => void | — | Called on each 'connection' event. |
addListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
addOnceListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
address(): TCPSocketAddress
Return the bound address of the underlying TCP server.
close(cb?: (err?: Error | null) => void): this
Stop the server from accepting new connections and close the ones it has
open, calling cb once it has closed. Each connection is sent a close
frame and dropped if it has not closed within five seconds, since
otherwise a peer that stops taking part would keep the server from ever
closing.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
cb? | (err?: Error | null) => void | — | Called once the underlying server has closed. |
connections: Set<WebSocket>
The connections the server has open. A socket handed over by an upgrade is
no longer the underlying HTTP server's to close, so these are tracked here
and closed by close.
emit<E extends keyof M>(name: E, ...args: M[E]): boolean
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
args | M[E] | — | — |
eventNames(): (keyof M)[]
getMaxListeners(): number
listenerCount<E extends keyof M>(name: E): number
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
listeners<E extends keyof M>(name: E): EventHandler<M[E]>[]
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
listening: boolean
true once the underlying server is bound and accepting connections.
off<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
on<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
once<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
prependListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
prependOnceListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
rawListeners<E extends keyof M>(name: E): EventHandler<M[E]>[]
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
ref(): this
Ref the underlying server so it keeps the event loop alive.
removeAllListeners<E extends keyof M>(name?: E): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name? | E | — | — |
removeListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | E | — | — |
fn | EventHandler<M[E]> | — | — |
Server.handshake(req: HTTPIncomingMessage, cb: (err: WebSocketError | null) => void): void
Perform the server side of the WebSocket opening handshake for req, writing the 101 upgrade response on socket and calling cb with a WebSocketError on failure.
Overloads:
Server.handshake(req: HTTPIncomingMessage, cb: (err: WebSocketError | null) => void): void
Server.handshake(req: HTTPIncomingMessage, socket: TCPSocket, cb: (err: WebSocketError | null) => void): void
Server.handshake(req: HTTPIncomingMessage, socket: TCPSocket, head: Buffer, cb: (err: WebSocketError | null) => void): voidParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
req | HTTPIncomingMessage | — | The incoming upgrade request. |
cb | (err: WebSocketError | null) => void | — | Called with a WebSocketError if the request is not a valid WebSocket upgrade, otherwise null. |
Server.WebSocketServerEvents
interface WebSocketServerEvents {
connection: [socket: WebSocket, req: HTTPIncomingMessage]
listening: []
close: []
error: [err: Error]
handshakeError: [err: WebSocketError, socket: TCPSocket]
}Server.WebSocketServerOptions
interface WebSocketServerOptions {
secure?: boolean
handshakeTimeout?: number
closeTimeout?: number
verifyClient?: (req: HTTPIncomingMessage) => boolean | Promise<boolean>
IncomingMessage?: typeof HTTPIncomingMessage
ServerResponse?: typeof HTTPServerResponse
maxPayload?: number
maxFragments?: number
idleTimeout?: number
}secure creates an HTTPS-backed server, accepting wss: connections, when true.
setMaxListeners(n: number): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n | number | — | — |
unref(): this
Unref the underlying server so it does not keep the event loop alive on its own.
bare-ws/global
Types
WebSocketConstructor
type WebSocketConstructor = typeof ws.SocketThe type of the Socket class, used to type the global WebSocket constructor.
bare-ws/constants
Functions
isValidStatus(code: number): boolean
Whether a close status may be sent in a close frame.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
code | number | — | The close status to check. |
Returns boolean — false outside the registered ranges, for the reserved 1004, and for the three codes only a local endpoint may report.
Constants and variables
EOL: string
The line ending (\r\n) used when writing the handshake response.
EOF: string
The blank-line terminator (\r\n\r\n) that ends the handshake response headers.
GUID: Buffer
The WebSocket protocol's fixed GUID, concatenated with the Sec-WebSocket-Key to compute the Sec-WebSocket-Accept digest.
opcode
opcode: {
CONTINUATION: number
TEXT: number
BINARY: number
CLOSE: number
PING: number
PONG: number
}The WebSocket frame opcodes (CONTINUATION, TEXT, BINARY, CLOSE, PING, PONG) as defined by RFC 6455.
status
status: {
NORMAL_CLOSURE: number
GOING_AWAY: number
PROTOCOL_ERROR: number
UNSUPPORTED_DATA: number
NO_STATUS_RECEIVED: number
ABNORMAL_CLOSURE: number
INVALID_PAYLOAD: number
POLICY_VIOLATION: number
MESSAGE_TOO_LARGE: number
MISSING_EXTENSION: number
INTERNAL_ERROR: number
SERVICE_RESTART: number
TRY_AGAIN_LATER: number
BAD_GATEWAY: number
TLS_HANDSHAKE_FAILURE: number
}The WebSocket close status codes defined by RFC 6455 and the IANA registry.
MAX_CONTROL_PAYLOAD_LENGTH: number
The largest payload a control frame may carry (125), as defined by RFC 6455.
MAX_PAYLOAD_LENGTH: number
The default maxPayload, in bytes.
MAX_FRAGMENTS: number
The default maxFragments.
MIN_PAYLOAD_CAPACITY: number
The smallest buffer a payload is read into before it doubles as it fills, in bytes.
HANDSHAKE_TIMEOUT: number
The default handshakeTimeout, in milliseconds.
IDLE_TIMEOUT: number
The default idleTimeout, in milliseconds.
CLOSE_TIMEOUT: number
How long a connection has to close of its own accord once it has been asked to, in milliseconds, before it is dropped.
bare-ws/errors
WebSocketError
WebSocketError
new WebSocketError(msg: string, code: string, status: number, fn?: WebSocketError, cause?: unknown)An error thrown for WebSocket protocol violations, carrying a code and a close status.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
code | string | — | The error code. |
status | number | — | The WebSocket close status associated with the error. |
fn? | WebSocketError | — | Optional function to omit from the top of the generated stack trace, passed to Error.captureStackTrace. |
cause? | unknown | — | The underlying cause of the error, if any. |
code: string
The error code, such as 'INVALID_OPCODE'.
status: number
The close status reported to the peer before the connection is failed, or 0 for a condition that never reaches the wire.
WebSocketError.CONNECTION_TIMEOUT(msg?: string): WebSocketError
No byte arrived from the peer within idleTimeout, and it did not answer the ping that followed.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'CONNECTION_TIMEOUT', for the caller to throw.
WebSocketError.EXPECTED_CONTINUATION(msg?: string): WebSocketError
A fragmented message's next frame was not a continuation frame.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'EXPECTED_CONTINUATION', for the caller to throw.
WebSocketError.EXPECTED_MASK(msg?: string): WebSocketError
A frame from a client was missing its required mask.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'EXPECTED_MASK', for the caller to throw.
WebSocketError.INVALID_ACCEPT_HEADER(msg?: string): WebSocketError
The Sec-WebSocket-Accept header in the server's handshake response did not match the expected digest.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_ACCEPT_HEADER', for the caller to throw.
WebSocketError.INVALID_CLOSE_PAYLOAD(msg?: string): WebSocketError
A close frame carried a single byte, which is too short to hold a status.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_CLOSE_PAYLOAD', for the caller to throw.
WebSocketError.INVALID_CLOSE_STATUS(msg?: string): WebSocketError
A close frame carried a status that no peer may send.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_CLOSE_STATUS', for the caller to throw.
WebSocketError.INVALID_CONTROL_PAYLOAD_LENGTH(msg?: string): WebSocketError
A control frame carried more than the 125 bytes RFC 6455 allows, whether received or passed to ping() or pong().
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_CONTROL_PAYLOAD_LENGTH', for the caller to throw.
WebSocketError.INVALID_ENCODING(msg?: string): WebSocketError
Data was written with an encoding other than buffer or utf8, or was not backed by bytes.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_ENCODING', for the caller to throw.
WebSocketError.INVALID_KEY_HEADER(msg?: string): WebSocketError
The Sec-WebSocket-Key header was missing or malformed.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_KEY_HEADER', for the caller to throw.
WebSocketError.INVALID_METHOD(msg?: string): WebSocketError
The handshake request used a method other than GET.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_METHOD', for the caller to throw.
WebSocketError.INVALID_OPCODE(msg?: string): WebSocketError
A frame was received with an opcode that is not TEXT or BINARY.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_OPCODE', for the caller to throw.
WebSocketError.INVALID_PAYLOAD_LENGTH(msg?: string): WebSocketError
A frame declared a payload length beyond the safe integer range.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_PAYLOAD_LENGTH', for the caller to throw.
WebSocketError.INVALID_PROTOCOL(msg?: string): WebSocketError
A URL was given with a scheme other than ws:, wss:, http: or https:.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_PROTOCOL', for the caller to throw.
WebSocketError.INVALID_UPGRADE_HEADER(msg?: string): WebSocketError
The Upgrade header was missing or not websocket.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_UPGRADE_HEADER', for the caller to throw.
WebSocketError.INVALID_UPGRADE_STATUS(msg?: string): WebSocketError
The server's handshake response carried a status other than 101.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_UPGRADE_STATUS', for the caller to throw.
WebSocketError.INVALID_UTF8(msg?: string): WebSocketError
A text frame or close reason was not well-formed UTF-8.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_UTF8', for the caller to throw.
WebSocketError.INVALID_VERSION_HEADER(msg?: string): WebSocketError
The Sec-WebSocket-Version header was neither 8 nor 13.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'INVALID_VERSION_HEADER', for the caller to throw.
WebSocketError.MESSAGE_TOO_LARGE(msg?: string): WebSocketError
A frame or reassembled message exceeded maxPayload.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'MESSAGE_TOO_LARGE', for the caller to throw.
WebSocketError.NETWORK_ERROR(msg: string, cause?: unknown): WebSocketError
The underlying HTTP request errored before the handshake completed.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
cause? | unknown | — | The underlying error. |
Returns WebSocketError — A WebSocketError with code set to 'NETWORK_ERROR', for the caller to throw.
WebSocketError.NOT_CONNECTED(msg?: string): WebSocketError
An operation, such as ping() or pong(), was attempted before the socket finished connecting, or after it closed.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'NOT_CONNECTED', for the caller to throw.
WebSocketError.TOO_MANY_FRAGMENTS(msg?: string): WebSocketError
A message was assembled from more fragments than maxFragments allows.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'TOO_MANY_FRAGMENTS', for the caller to throw.
WebSocketError.UNEXPECTED_CLOSE(msg?: string): WebSocketError
The connection went away before a close frame had been exchanged, so part of a message may have been lost.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_CLOSE', for the caller to throw.
WebSocketError.UNEXPECTED_CONTINUATION(msg?: string): WebSocketError
A continuation frame was received without a preceding fragmented frame.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_CONTINUATION', for the caller to throw.
WebSocketError.UNEXPECTED_CONTROL(msg?: string): WebSocketError
A control frame was fragmented, which RFC 6455 does not allow.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_CONTROL', for the caller to throw.
WebSocketError.UNEXPECTED_EXTENSION(msg?: string): WebSocketError
The server's handshake response negotiated an extension, none of which this side ever offers.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_EXTENSION', for the caller to throw.
WebSocketError.UNEXPECTED_MASK(msg?: string): WebSocketError
A frame from a server carried a mask, which only a client may set.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_MASK', for the caller to throw.
WebSocketError.UNEXPECTED_PROTOCOL(msg?: string): WebSocketError
The server's handshake response named a subprotocol the client did not offer.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_PROTOCOL', for the caller to throw.
WebSocketError.UNEXPECTED_RSV1(msg?: string): WebSocketError
A frame was received with the reserved RSV1 bit set.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_RSV1', for the caller to throw.
WebSocketError.UNEXPECTED_RSV2(msg?: string): WebSocketError
A frame was received with the reserved RSV2 bit set.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_RSV2', for the caller to throw.
WebSocketError.UNEXPECTED_RSV3(msg?: string): WebSocketError
A frame was received with the reserved RSV3 bit set.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UNEXPECTED_RSV3', for the caller to throw.
WebSocketError.UPGRADE_REJECTED(msg?: string): WebSocketError
A server's verifyClient refused the handshake.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg? | string | — | The error message. |
Returns WebSocketError — A WebSocketError with code set to 'UPGRADE_REJECTED', for the caller to throw.
See also
- Builds on
bare-crypto,bare-events,bare-http1,bare-https, andbare-stream. - It's pure JavaScript. Sockets are
bare-streamduplex streams that perform the WebSocket handshake over an HTTP or HTTPS request. bare-tcp— the socket layer WebSocket connections run over.bare-fetch— a request/response HTTP client for the non-persistent case.- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.
Last updated on