当前位置:网站首页>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
}
}
边栏推荐
- Real time waveform calculation function of Waveform Recorder mr6000
- Command pattern
- 机器人强化学习——第一人称 VS 第三人称
- Use VS to create a static link library Lib and use
- What are the circular statements of MySQL
- 泰克DPO4104数字荧光示波器技术参数
- BERT和ViT简介
- Leetcode theme [array] -219- duplicate Element II exists
- be based on. NETCORE development blog project starblog - (13) add friendship link function
- Memo pattern
猜你喜欢

Structure training camp module II operation

【IoT】公众号“简一商业”更名为“产品人卫朋”说明

网络设备设置/取消console口登陆单独密码

What are the ways to simulate and burn programs? (including common tools and usage)

Open source demo| you draw and I guess -- make your life more interesting

What are the MySQL database constraint types

Composite pattern

Introduction to Photoshop (the first case)

《软件体系结构》期末复习总结

Sailing with karmada: multi cluster management of massive nodes
随机推荐
be based on. NETCORE development blog project starblog - (13) add friendship link function
What are the basic usage methods of MySQL
How to quickly install MySQL 5.7.17 under CentOS 6.5
Microsoft Pinyin IME personal preferences
Facade pattern
MySQL subquery
Domain name hack
What are the circular statements of MySQL
[structural mechanics] the reason why the influence line under joint load is different from that under direct load
D parallel and rotator
docker 创建的 mysql8 怎么改密码
Research Report on the overall scale, major manufacturers, major regions, products and applications of electric hydrofoil surfboards in the global market in 2022
笔记本访问台式机的共享磁盘
See how I do it step by step (I)
Real time waveform calculation function of Waveform Recorder mr6000
《软件体系结构》期末复习总结
机器人强化学习——Transferring End-to-End Visuomotor Control from Simulation to RealWorld (CoRL 2017)
Introduction to Bert and Vit
Go Foundation (I)
Distributed transaction Seata