mist/src/worker.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-11-12 04:13:05 +08:00
const ports = []
self.onconnect = (e) => {
console.log('worker.onconnect:', e)
for (const port of e.ports) {
port.onmessage = (e) => {
console.log('onmessage:', ports)
ports.forEach(item => {
item.postMessage(e.data)
})
}
port.onclose = () => {
console.log('onclose:', ports)
var index = ports.indexOf(port)
if (index !== -1) {
ports.splice(index, 1)
}
}
ports.push(port)
}
}
//// 获取当前浏览器是 http 还是 https 以判断使用 ws 还是 wss
//const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
//const host = window.location.host
//
//// 向服务器建立 websocket 连接
//const ws = new WebSocket(`${protocol}://${host}/online?name=client`)
//ws.onopen = () => {
// console.log('ws.onopen')
//}
//ws.onclose = () => {
// console.log('ws.onclose')
//}
//ws.onerror = () => {
// console.log('ws.onerror')
//}
//ws.onmessage = message => {
// console.log('ws.onmessage:', message)
// const data = JSON.parse(message.data)
// console.log('message:', data)
//}