MessageChannle 会生成两个 port
const channel = new MessageChannel();
const { port1, port2 } = channel;
oBtn1.addEventListener('click', handleSendMessage1);
oBtn2.addEventListener('click', handleSendMessage2);
function handleSendMessage1 () {
port1.postMessage('I am PORT-1');
}
function handleSendMessage2 () {
port2.postMessage('I am PORT-2');
}
port1.onmessage = (e) => {
console.log(e);
}
port2.onmessage = (e) => {
console.log(e);
}