MessageChannel:port2 属性

Baseline Widely available

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

备注: 此特性在 Web Worker 中可用。

MessageChannel 接口的 port2 只读属性返回消息频道的第二个端口——即连接到通道另一端上下文的端口,消息最初将发送到这个端口。

一个 MessagePort 对象,它是消息频道的第二个端口,即连接到消息频道另一端上下文的端口。

示例

在以下的代码块中,你可以看到使用 MessageChannel() 构造函数创建的新消息频道。当 IFrame 加载完成后,我们使用 Window.postMessage()port2 传递给 IFrame,并附带一条消息。然后 handleMessage 处理程序响应从 IFrame 发送回来的消息(使用 onmessage),并将其放入一个段落中。同时监听 port1 以检查何时接收到消息。

js
const channel = new MessageChannel();
const para = document.querySelector("p");

const ifr = document.querySelector("iframe");
const otherWindow = ifr.contentWindow;

ifr.addEventListener("load", iframeLoaded, false);

function iframeLoaded() {
  otherWindow.postMessage("来自主页的问候!", "*", [channel.port2]);
}

channel.port1.onmessage = handleMessage;
function handleMessage(e) {
  para.innerHTML = e.data;
}

有关完整的运行示例,请参阅我们在 GitHub 上的频道消息传递基本演示也可以实时运行它

规范

Specification
HTML
# dom-messagechannel-port2-dev

浏览器兼容性

BCD tables only load in the browser

参见