--- title: WebSocket.onclose slug: Web/API/WebSocket/close_event translation_of: Web/API/WebSocket/onclose original_slug: Web/API/WebSocket/onclose ---

{{APIRef("Web Sockets API")}}

WebSocket.onclose 属性返回一个事件监听器,这个事件监听器将在 WebSocket 连接的{{domxref("WebSocket.readyState","readyState")}} 变为 CLOSED时被调用,它接收一个名字为“close”的 {{domxref("CloseEvent")}} 事件。

语法

WebSocket.onclose = function(event) {
  console.log("WebSocket is closed now.");
};

一个{{domxref("EventListener")}}.

文档

Specification Status Comment
{{SpecName('HTML WHATWG', '#handler-websocket-onclose', 'WebSocket: onclose')}} {{Spec2('HTML WHATWG')}} Initial definition