MessageChannle 会生成两个 port

    1. const channel = new MessageChannel();
    2. const { port1, port2 } = channel;
    3. oBtn1.addEventListener('click', handleSendMessage1);
    4. oBtn2.addEventListener('click', handleSendMessage2);
    5. function handleSendMessage1 () {
    6. port1.postMessage('I am PORT-1');
    7. }
    8. function handleSendMessage2 () {
    9. port2.postMessage('I am PORT-2');
    10. }
    11. port1.onmessage = (e) => {
    12. console.log(e);
    13. }
    14. port2.onmessage = (e) => {
    15. console.log(e);
    16. }