From 62a2dc6f1d0e342ce65b269de1a91008cea9fa5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Wed, 27 Sep 2023 14:26:12 +0800 Subject: [PATCH] init --- README.md | 11 +++ index.js | 50 ++++++++++++ package.json | 20 +++++ public/index.html | 196 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 index.js create mode 100644 package.json create mode 100644 public/index.html diff --git a/README.md b/README.md index c7c9c0e..93d2d0e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # webrtc webrtc 实现的 p2p 信道 + +// 能获取所有在线设备列表 +// 随机连接至四个设备, 且按效率扩展收缩 +// 将数据拆解同时向多台设备分发, 对端接收后再次分发 +// 需要确保全部设备获得全部数据, 每台设备至少一半不重复 +// 五色 +// 单向链 +// 固定填位(矩阵) +// [a1, b1, c1, d1, e1] +// [a2, b2, c2, d2, e2] +// [a3, b3, c3, d3, e3] diff --git a/index.js b/index.js new file mode 100644 index 0000000..83aab21 --- /dev/null +++ b/index.js @@ -0,0 +1,50 @@ +import express from 'express' +import expressWs from 'express-ws' + +const app = express() +const wsInstance = expressWs(app) +app.use(express.static('public')) + +// Websocket 处理 webRTC 信令 +app.ws('/webrtc/:channel', (ws, req) => { + ws.id = req.headers['sec-websocket-key'] + ws.channel = req.params.channel + // 设备离开频道时广播给所有在线设备 + ws.on('close', () => { + console.log(ws.id, '设备离开频道:', ws.channel, wsInstance.getWss().clients.size) + wsInstance.getWss().clients.forEach(client => { + if (client !== ws && client.readyState === 1 && client.channel === ws.channel) { + client.send(JSON.stringify({ type: 'pull', id: ws.id, channel: ws.channel })) + } + }) + }) + // 设备发生错误时广播给所有在线设备 + ws.on('error', () => { + console.log(ws.id, '设备发生错误:', ws.channel, wsInstance.getWss().clients.size) + wsInstance.getWss().clients.forEach(client => { + if (client !== ws && client.readyState === 1 && client.channel === ws.channel) { + client.send(JSON.stringify({ type: 'error', id: ws.id, channel: ws.channel })) + } + }) + }) + // 设备发送信令时转发给指定在线设备 + ws.on('message', message => { + console.log(ws.id, '设备发送信令:', ws.channel, wsInstance.getWss().clients.size) + const { id } = JSON.parse(message) + wsInstance.getWss().clients.forEach(client => { + if (client !== ws && client.readyState === 1 && client.channel === ws.channel && client.id === id) { + client.send(message) + } + }) + }) + // 设备加入频道时广播给所有在线设备(也获取所有在线设备) + console.log(ws.id, '设备加入频道:', ws.channel, wsInstance.getWss().clients.size) + wsInstance.getWss().clients.forEach(client => { + if (client !== ws && client.readyState === 1 && client.channel === ws.channel) { + client.send(JSON.stringify({ type: 'push', id: ws.id, channel: ws.channel })) + ws.send(JSON.stringify({ type: 'push', id: client.id, channel: client.channel })) + } + }) +}) + +app.listen(3000, () => console.log('Server started on port 3000')) diff --git a/package.json b/package.json new file mode 100644 index 0000000..236db8e --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "webrtc", + "version": "1.0.0", + "description": "", + "type": "module", + "main": "index.js", + "scripts": { + "dev": "nodemon index.js", + "start": "node index.js" + }, + "author": "", + "license": "GPL-3.0-or-later", + "devDependencies": { + "nodemon": "^3.0.1" + }, + "dependencies": { + "express": "^4.18.2", + "express-ws": "^5.0.2" + } +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..5ce65d7 --- /dev/null +++ b/public/index.html @@ -0,0 +1,196 @@ + + + + + + webRTC + + + +
+

webRTC

+

message

+
+ + + + + \ No newline at end of file