WebSocket: message event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

message 事件会在 WebSocket 接收到新消息时被触发。

起泡(Bubbles)
可取消
接口 MessageEvent
事件处理程序属性 onmessage

例子

js
// 创建一个 WebSocket 连接
const socket = new WebSocket("ws://localhost:8080");

// 监听消息
socket.addEventListener("message", function (event) {
  console.log("Message from server ", event.data);
});

规范

Specification
WebSockets Standard
# dom-websocket-onmessage

浏览器兼容性

BCD tables only load in the browser

另看