WebSocket
Types
binaryType
type binaryType = WebSocketsTypes.binaryTypecloseEvent
type closeEvent = WebSocketsTypes.closeEvent = { type_: EventTypes.eventType, target: Null.t<EventTypes.eventTarget>, currentTarget: Null.t<EventTypes.eventTarget>, eventPhase: int, bubbles: bool, cancelable: bool, defaultPrevented: bool, composed: bool, isTrusted: bool, timeStamp: float, wasClean: bool, code: int, reason: string,}Record fields
type_
Returns the type of event, e.g. "click", "hashchange", or "submit". Read more on MDN
target
Returns the object to which event is dispatched (its target). Read more on MDN
currentTarget
Returns the object whose event listener's callback is currently being invoked. Read more on MDN
eventPhase
Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. Read more on MDN
bubbles
Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. Read more on MDN
cancelable
Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. Read more on MDN
defaultPrevented
Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. Read more on MDN
composed
Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. Read more on MDN
isTrusted
Returns true if event was dispatched by the user agent, and false otherwise. Read more on MDN
timeStamp
Returns the event's timestamp as the number of milliseconds measured relative to the time origin. Read more on MDN
reason
Returns the WebSocket connection close reason provided by the server. Read more on MDN
messageEvent
type messageEvent<'t> = WebSocketsTypes.messageEvent<'t> = { type_: EventTypes.eventType, target: Null.t<EventTypes.eventTarget>, currentTarget: Null.t<EventTypes.eventTarget>, eventPhase: int, bubbles: bool, cancelable: bool, defaultPrevented: bool, composed: bool, isTrusted: bool, timeStamp: float, data: 't, origin: string, lastEventId: string, source: Null.t<WebSocketsTypes.messageEventSource>, ports: array<ChannelMessagingTypes.messagePort>,}Record fields
type_
Returns the type of event, e.g. "click", "hashchange", or "submit". Read more on MDN
target
Returns the object to which event is dispatched (its target). Read more on MDN
currentTarget
Returns the object whose event listener's callback is currently being invoked. Read more on MDN
eventPhase
Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. Read more on MDN
bubbles
Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. Read more on MDN
cancelable
Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. Read more on MDN
defaultPrevented
Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. Read more on MDN
composed
Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. Read more on MDN
isTrusted
Returns true if event was dispatched by the user agent, and false otherwise. Read more on MDN
timeStamp
Returns the event's timestamp as the number of milliseconds measured relative to the time origin. Read more on MDN
origin
Returns the origin of the message, for server-sent events and cross-document messaging. Read more on MDN
source
Returns the WindowProxy of the source window, for cross-document messaging, and the MessagePort being attached, in the connect event fired at SharedWorkerGlobalScope objects. Read more on MDN
ports
Returns the MessagePort array sent with the message, for cross-document messaging and channel messaging. Read more on MDN
messageEventSource
type messageEventSource = WebSocketsTypes.messageEventSourcet
type t = WebSocketsTypes.webSocket = { url: string, readyState: int, bufferedAmount: int, extensions: string, protocol: string, mutable binaryType: WebSocketsTypes.binaryType,}Record fields
url
Returns the WebApiURL that was used to establish the WebSocket connection. Read more on MDN
readyState
Returns the state of the WebSocket object's connection. It can have the values described below. Read more on MDN
bufferedAmount
Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.
If the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.) Read more on MDN
protocol
Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation. Read more on MDN
binaryType
Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:
Can be set, to change how binary data is returned. The default is "blob". Read more on MDN
Values
addEventListener
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture. Read more on MDN
let addEventListener: ( t, EventTypes.eventType, EventTypes.eventListener<'event>, ~options: EventTypes.addEventListenerOptions=?,) => unitParameters
t EventTypes.eventType EventTypes.eventListener option< EventTypes.addEventListenerOptions >Return type
unitaddEventListenerWithCapture
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture. Read more on MDN
let addEventListenerWithCapture: ( t, EventTypes.eventType, EventTypes.eventListener<'event>,) => unitasEventTarget
let asEventTarget: t => EventTypes.eventTargetclose
Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason. Read more on MDN
let close: (t, ~code: int=?, ~reason: string=?) => unitdispatchEvent
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise. Read more on MDN
let dispatchEvent: (t, EventTypes.event) => boolfromURL
fromURL(~url: string, ~protocols: string=?)
Creates a new WebSocket from a URL and an optional single protocol.
let socket = WebSocket.fromURL(~url="wss://example.com/socket")
let fromURL: (~url: string, ~protocols: string=?) => tfromURLWithProtocols
fromURLWithProtocols(~url: string, ~protocols: array<string>)
Creates a new WebSocket from a URL and multiple protocols.
let socket =
WebSocket.fromURLWithProtocols(
~url="wss://example.com/socket",
~protocols=["chat", "superchat"],
)
let fromURLWithProtocols: (~url: string, ~protocols: array<string>) => tremoveEventListener
Removes the event listener in target's event listener list with the same type, callback, and options. Read more on MDN
let removeEventListener: ( t, EventTypes.eventType, EventTypes.eventListener<'event>, ~options: EventTypes.eventListenerOptions=?,) => unitParameters
t EventTypes.eventType EventTypes.eventListener option< EventTypes.eventListenerOptions >Return type
unitremoveEventListenerUseCapture
Removes the event listener in target's event listener list with the same type, callback, and options. Read more on MDN
let removeEventListenerUseCapture: ( t, EventTypes.eventType, EventTypes.eventListener<'event>,) => unitsend
Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN
let send: (t, DataView.t) => unitsendArrayBuffer
Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN
let sendArrayBuffer: (t, ArrayBuffer.t) => unitsendBlob
Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN
let sendBlob: (t, Blob.t) => unitsendString
Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN
let sendString: (t, string) => unit