interface CloseEvent
extends Event
The CloseEvent
interface represents an event that occurs when a WebSocket
connection is closed.
This event is sent to the client when the connection is closed, providing information about
why the connection was closed through the code
, reason
, and wasClean
properties.
Examples #
#
// Handling a close event
ws.addEventListener("close", (event: CloseEvent) => {
console.log(`Connection closed with code ${event.code}`);
console.log(`Reason: ${event.reason}`);
console.log(`Clean close: ${event.wasClean}`);
if (event.code === 1006) {
console.log("Connection closed abnormally");
}
});
Properties #
See #
variable CloseEvent
Constructor interface for creating CloseEvent
instances.
Examples #
#
// Creating a custom close event
const event = new CloseEvent("close", {
code: 1000,
reason: "Normal closure",
wasClean: true,
});
// Dispatching the event
myWebSocket.dispatchEvent(event);
Properties #
#prototype: CloseEvent
readonly