From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/websocket/message_event/index.html | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 files/zh-cn/web/api/websocket/message_event/index.html (limited to 'files/zh-cn/web/api/websocket/message_event') diff --git a/files/zh-cn/web/api/websocket/message_event/index.html b/files/zh-cn/web/api/websocket/message_event/index.html new file mode 100644 index 0000000000..350aac9424 --- /dev/null +++ b/files/zh-cn/web/api/websocket/message_event/index.html @@ -0,0 +1,76 @@ +--- +title: 'WebSocket: message event' +slug: Web/API/WebSocket/message_event +translation_of: Web/API/WebSocket/message_event +--- +
+

{{APIRef}}

+ +

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

+ + + + + + + + + + + + + + + + + + + + +
起泡(Bubbles)
可取消
接口{{domxref("MessageEvent")}}
事件处理程序属性{{ domxref("WebSocket.onmessage","onmessage")}}
+ +

例子

+ +
// 创建一个 WebSocket 连接
+const socket = new WebSocket('ws://localhost:8080');
+
+// 监听消息
+socket.addEventListener('message', function (event) {
+    console.log('Message from server ', event.data);
+});
+ +

规范

+ + + + + + + + + + + + + + +
规范状态
{{SpecName("HTML WHATWG", "web-sockets.html#event-message", "WebSocket message")}}{{Spec2("HTML WHATWG")}}
+ +

浏览器兼容性

+ +

{{Compat("api.WebSocket.message_event")}}

+ +

另看

+ + +
+ + -- cgit v1.2.3-54-g00ecf