纯属开脑洞笑料,不要当真
该脑洞是利用localStorage api和StorageEvent建立的,支持最大5m的信息传输233333
// main.html
var PageWorkerManager = {
num: 4,
pools: [],
init: function () {
for (var i = 0, len = this.num; i < len, i++) {
window.open('MyWorkerPage.html?id=' + i, '_blank');
this.pools.push({
id: i,
connect: false
})
}
window.addEventListener('storage', this.handler.bind(this), false)
},
handler: function (evt) {
this['on' + evt.key](evt.newValue, evt.oldValue);
},
oninited: function (id) {
this.pools[id].connect = true
},
onclose: function (id) {
this.pools[id].connect = false
},
postMessage: function (event, msg) {
localStorage.setItem(event, msg)
}
}
// MyWorkerPage.html
var PageWorker = {
id: parseInt(this.getParams('id'), 10),
init: function () {
localStorage.setItem('inited', this.id)
window.addEventListener('storage', this.handler.bind(this), false)
window.addEventListener('beforeunload', this.beforeunload.bind(this), false)
},
handler: function (evt) {
this['on' + evt.key](evt.newValue, evt.oldValue);
},
beforeunload: function () {
localStorage.setItem('close', this.id)
},
postMessage: function (event, msg) {
localStorage.setItem(event, msg)
}
}