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 # dom-websocket-onmessage |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
message event |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.