当前位置:网站首页>JS messagechannel transport
JS messagechannel transport
2022-06-29 05:13:00 【Magic uncle】
window.MessageChannel It is also an asynchronous operation provided by the browser API, Belongs to macro task
This api Will create a pipe , The two ends of the pipe represent a messagePort, Can pass through portMessage Send data to the other party , adopt onmessage To receive the data sent by the other party 
Basic use
const ch = new MessageChannel()
const port1 = ch.port1
const port2 = ch.port2
port1.onmessage = function(d) {
console.log(`port1 The message received is :${
d.data}`)
}
port2.onmessage = function(d) {
console.log(`port2 The message received is :${
d.data}`)
}
// Send a message
port1.portMessage('port1 Message sent ')
port2.portMessage('port2 Message sent ')
Application scenarios
Let two web worker Can pass MessageChannel signal communication , That is to achieve multi-threaded communication
// index.html
<script>
var w1 = new Worker("worker1.js");
var w2 = new Worker("worker2.js");
var ch = new MessageChannel();
w1.postMessage("port1", [ch.port1]); // The second parameter will not be invalidated by code conversion
w2.postMessage("port2", [ch.port2]);
w2.onmessage = function(e) {
console.log(e.data);
}
</script>
====worker1.js
// worker1.js
self.onmessage = function(e) {
const port = e.ports[0];
port.postMessage("this is from worker1") //woker1 Pass through port1 Send to port2
}
====worker2.js
// worker2.js
self.onmessage = function(e) {
const port = e.ports[0];
port.onmessage = function(e) {
postMessage(e.data) //woker2 In the listening woker1 in port1 Data transferred , Then trigger itself woker Monitor function of
}
}
边栏推荐
- What is an anonymous inner class and how to use it
- Open a new ecological posture | use WordPress remote attachment to store to Cos
- Embedded RTOS
- What is the method of connection query in MySQL
- Microsoft Pinyin IME personal preferences
- How to test electronic components with a multimeter
- Notebook access desktop shared disk
- Go Foundation (I)
- Research Report on the overall scale, major manufacturers, major regions, products and applications of power battery laser welding machines in the global market in 2022
- Open source demo| you draw and I guess -- make your life more interesting
猜你喜欢

On February 15, the market hot money operation and the dragon and tiger list

Use VS to create a static link library Lib and use

Cipher

How to use thread stack location

Distributed transaction Seata

I haven't encountered these three problems. I'm sorry to say that I used redis

轻松入门自然语言处理系列 专题7 基于FastText的文本分类

Tcapulusdb Jun · industry news collection (III)

Mediator pattern

5000+ 字解读 | 产品经理:如何做好元器件选型?
随机推荐
20年秦皇島D - Exam Results(二分+思維,附易錯數據)
MySQL subquery
[high concurrency] deeply analyze the callable interface
Quelles sont les méthodes de simulation et de gravure des programmes? (comprend les outils communs et la façon dont ils sont utilisés)
Domain name hack
Flyweight pattern
Résultats D - exam de Qinhuangdao au cours des 20 dernières années
安捷伦数字万用表软件NS-Multimeter,实时数据采集数据自动保存
20年秦皇岛D - Exam Results(二分+思维,附易错数据)
汉泰示波器软件|汉泰示波器上位机软件NS-Scope,任意添加测量数据
轻松入门自然语言处理系列 专题7 基于FastText的文本分类
Introduction to Bert and Vit
Research Report on the overall scale, major manufacturers, major regions, products and application segments of 5g modules of the Internet of things in the global market in 2022
Research Report on the overall scale, major manufacturers, major regions, product and application segmentation of GSM and GPRS modules in the global market in 2022
What are the ways to simulate and burn programs? (including common tools and usage)
证券开户安全么,有没有什么危险呢
Tcapulusdb Jun · industry news collection (III)
Research Report on global market segmentation based on condition based maintenance (CBM) overall scale, major enterprises, major regions, products and applications in 2022
Mysql 中的 mvcc原理
C語言用 printf 打印 《愛心》《火星撞地球》等,不斷更新