支持自定义名字
This commit is contained in:
parent
656bc9c504
commit
b671c459d4
5
index.js
5
index.js
@ -10,6 +10,7 @@ app.use(express.static('public'))
|
|||||||
app.ws('/webrtc/:channel', (ws, req) => {
|
app.ws('/webrtc/:channel', (ws, req) => {
|
||||||
ws.id = req.headers['sec-websocket-key']
|
ws.id = req.headers['sec-websocket-key']
|
||||||
ws.channel = req.params.channel
|
ws.channel = req.params.channel
|
||||||
|
ws.name = req.query.name
|
||||||
// 设备离开频道时广播给所有在线设备
|
// 设备离开频道时广播给所有在线设备
|
||||||
ws.on('close', () => {
|
ws.on('close', () => {
|
||||||
console.log(ws.id, '设备离开频道:', ws.channel, wsInstance.getWss().clients.size)
|
console.log(ws.id, '设备离开频道:', ws.channel, wsInstance.getWss().clients.size)
|
||||||
@ -42,8 +43,8 @@ app.ws('/webrtc/:channel', (ws, req) => {
|
|||||||
console.log(ws.id, '设备加入频道:', ws.channel, wsInstance.getWss().clients.size)
|
console.log(ws.id, '设备加入频道:', ws.channel, wsInstance.getWss().clients.size)
|
||||||
wsInstance.getWss().clients.forEach(client => {
|
wsInstance.getWss().clients.forEach(client => {
|
||||||
if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
|
if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
|
||||||
client.send(JSON.stringify({ type: 'push', id: ws.id, channel: ws.channel }))
|
client.send(JSON.stringify({ type: 'push', id: ws.id, name: ws.name, channel: ws.channel }))
|
||||||
ws.send(JSON.stringify({ type: 'list', id: client.id, channel: client.channel }))
|
ws.send(JSON.stringify({ type: 'list', id: client.id, name: client.name, channel: client.channel }))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
import { List, ListItem } from './weigets.js'
|
import { List, ListItem } from './weigets.js'
|
||||||
|
|
||||||
export default class ClientList {
|
export default class ClientList {
|
||||||
constructor({ channels = {}, EventListeners = {} }) {
|
constructor({ channels = {}, EventListeners = {}, name }) {
|
||||||
this.channels = channels
|
this.channels = channels
|
||||||
this.EventListeners = EventListeners
|
this.EventListeners = EventListeners
|
||||||
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
||||||
const host = window.location.host
|
const host = window.location.host
|
||||||
this.websocket = new WebSocket(`${protocol}://${host}/webrtc/music`)
|
this.websocket = new WebSocket(`${protocol}://${host}/webrtc/music?name=${name}`)
|
||||||
this.clientlist = []
|
this.clientlist = []
|
||||||
this.ul = List({})
|
this.ul = List({})
|
||||||
document.body.appendChild(this.ul)
|
document.body.appendChild(this.ul)
|
||||||
|
|
||||||
this.websocket.onmessage = async event => {
|
this.websocket.onmessage = async event => {
|
||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.data)
|
||||||
const channels_init = (webrtc) => {
|
const channels_init = (webrtc) => {
|
||||||
|
@ -22,6 +22,21 @@
|
|||||||
const list = await store.getAll()
|
const list = await store.getAll()
|
||||||
console.log('本地音乐列表:', list)
|
console.log('本地音乐列表:', list)
|
||||||
|
|
||||||
|
// 读取本地用户名(本地缓存)
|
||||||
|
const name = localStorage.getItem('username')
|
||||||
|
if (!name) {
|
||||||
|
localStorage.setItem('username', `user-${Math.random().toString(36).substr(2)}`)
|
||||||
|
}
|
||||||
|
// 设置自己的主机名
|
||||||
|
const nameInput = document.createElement('input')
|
||||||
|
nameInput.type = 'text'
|
||||||
|
nameInput.placeholder = '请设置你的名字'
|
||||||
|
nameInput.onchange = event => {
|
||||||
|
localStorage.setItem('username', event.target.value)
|
||||||
|
window.location.reload() // 简单刷新页面
|
||||||
|
}
|
||||||
|
document.body.appendChild(nameInput)
|
||||||
|
|
||||||
// 初始化音乐列表(加入本地缓存)
|
// 初始化音乐列表(加入本地缓存)
|
||||||
const musicList = new MusicList({ list })
|
const musicList = new MusicList({ list })
|
||||||
musicList.on('remove', item => {
|
musicList.on('remove', item => {
|
||||||
@ -64,7 +79,7 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 初始化客户端列表
|
// 初始化客户端列表
|
||||||
const clientList = new ClientList({})
|
const clientList = new ClientList({ name })
|
||||||
|
|
||||||
// 缓冲分片发送
|
// 缓冲分片发送
|
||||||
const CHUNK_SIZE = 1024 * 128 // 每个块的大小为128KB
|
const CHUNK_SIZE = 1024 * 128 // 每个块的大小为128KB
|
||||||
|
Loading…
Reference in New Issue
Block a user