本文共 1219 字,大约阅读时间需要 4 分钟。
The Channel Messaging API is a great way to send 1-to-1 messages from a window to an iframe, from a window to a Web Worker, and so on.
Channel Messaging API是一种从窗口向iframe,从窗口向Web Worker等发送一对一消息的好方法。
The BroadcastChannel API can be used to send 1-to-many messages, communicating to multiple entities at the same time.
BroadcastChannel API可用于发送一对多消息,同时与多个实体进行通信。
You start by initializing a BroadcastChannel
object:
首先初始化一个BroadcastChannel
对象:
const channel = new BroadcastChannel ( 'thechannel' )
To send a message on the channel you use the postMessage()
method:
要在频道上发送消息,请使用postMessage()
方法:
channel . postMessage ( 'Hey!' )
A message can be any of those supported values:
消息可以是以下任何受支持的值:
, , objects
, , 对象
, , objects
, , 对象
, objects
, 对象
objects
对象
and objects
和对象
To receive messages from the channel, listen to the message
event:
要从频道接收消息,请收听message
事件:
channel . onmessage = event => { console . log ( 'Received' , event . data )}
This event is fired for all listeners, except the one that is sending the message.
除发送消息的侦听器之外的所有侦听器均触发此事件。
You can close the channel using:
您可以使用以下方法关闭频道:
channel . close ()
翻译自:
转载地址:http://ioqgb.baihongyu.com/