SharedWorker

This commit is contained in:
2023-11-12 04:13:05 +08:00
parent 05c3507631
commit ce943faa6e
5 changed files with 137 additions and 2 deletions

12
index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>薄雾</title>
</head>
<body>
<h1>薄雾</h1>
<script type="module" src="./src/main.js"></script>
</body>
</html>

55
index.js Normal file
View File

@ -0,0 +1,55 @@
import express from 'express'
import expressWs from 'express-ws'
const app = express()
const wsInstance = expressWs(app)
app.use(express.static('dist'))
app.use(express.json())
app.ws('/online', (ws, req) => {
ws.id = req.headers['sec-websocket-key']
ws.channel = req.params.channel
ws.name = req.query.name
console.log('ws.name:', ws.name)
ws.on('close', () => {
console.log(ws.id, '设备离开频道:', ws.channel, wsInstance.getWss().clients.size)
})
ws.on('error', () => {
console.log(ws.id, '设备发生错误:', ws.channel, wsInstance.getWss().clients.size)
})
ws.on('message', message => {
console.log(ws.id, '设备发送消息:', ws.channel, wsInstance.getWss().clients.size)
const data = JSON.parse(message)
console.log('message:', data)
// 设备告知伺服器开始为某些资源做种
// 服务器不参与设备之间通信, 只作有效性统计
// 设备与8个设备建立连接
// 连接不足时, 向剩余设备请求邻接表
// 从邻接表中选择合适的设备建立连接
// 请求资源时, 遍历每次向随机2个设备询问, 如果不提供, 则向邻接表中的设备询问
// 如果命中率低, 考虑扩大连接表, 命中率足够时, 考虑缩小连接表
// 总之先统计资源分布情况
// 设备获取某些资源的分布节点 -> 随后设备自行连接节点获取数据
})
//// 设备加入频道时广播给所有在线设备(也获取所有在线设备)
//console.log(ws.id, '设备加入频道:', ws.channel, wsInstance.getWss().clients.size)
//const list = []
//wsInstance.getWss().clients.forEach(client => {
// if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
// console.log(ws.name, '广播给在线设备:', client.name)
// client.send(JSON.stringify({ type: 'push', user: { id: ws.id, name: ws.name }, channel: ws.channel }))
// list.push({ id: client.id, name: client.name })
// }
//})
//ws.send(JSON.stringify({ type: 'list', list }))
})
// WEBHOOK 处理 Git 事件
app.post('/webhook', (req, res) => {
console.log('WEBHOOK:' + new Date().toLocaleString())
exec('git pull;npm i;npm run build')
return res.json({ success: true })
})
app.listen(9999, () => console.log('Server started on port 9999'))

View File

@ -3,8 +3,12 @@
"version": "1.0.0",
"description": "薄雾 用户算力资源检测",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"start": "node index.js"
},
"repository": {
"type": "git",
@ -13,6 +17,10 @@
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
"express": "^4.18.2",
"express-ws": "^5.0.2"
},
"devDependencies": {
"vite": "^4.4.11"
}
}

18
src/main.js Normal file
View File

@ -0,0 +1,18 @@
if (typeof SharedWorker === "undefined") {
alert('当前浏览器不支持webworker')
}
// 检查是否存在其它标签页, 如果存在不必建立 websocket 连接, 如果不存在, 则建立 websocket 连接
const worker = new SharedWorker('/src/worker.js')
worker.port.onmessage = (e) => {
console.log('worker.port.onmessage:', e.data)
}
worker.port.start()
const button = document.createElement('button')
button.innerText = 'click'
button.onclick = () => {
worker.port.postMessage('hello, worker')
}
document.body.appendChild(button)

42
src/worker.js Normal file
View File

@ -0,0 +1,42 @@
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)
//}