Working datachannel, youtube support & server fix

This commit is contained in:
Thomas
2020-07-29 19:03:11 +02:00
parent 927fa2d1a2
commit e8247a1ba3
9 changed files with 295 additions and 16 deletions

View File

@@ -3,7 +3,16 @@ const state = {
roomStatus: {
roomName: '',
roomCode: '',
current: '',
player: {
timeCode: 0,
playing: true
},
current: {
link: '',
title: '',
votes: 0,
voters: []
},
playlist: []
}
}
@@ -23,6 +32,35 @@ const actions = {
},
setAdmin ({ commit }) {
commit('SET_ADMIN')
},
vote ({ commit, dispatch, state }, { link, isPositive, voterName }) {
console.log('vote on ' + link + ' (' + isPositive + ') by ' + voterName)
if (isPositive) {
commit('ADD_VOTE', {
link: link,
voterName: voterName
})
} else {
commit('REMOVE_VOTE', {
link: link,
voterName: voterName
})
}
dispatch('rtc/broadcast', { message: state.roomStatus, type: 'status' }, { root: true })
},
setCurrent ({ commit, dispatch }, { playerStatus, timeCode }) {
switch (playerStatus) {
case 0:
commit('CURRENT_END')
break
case 1:
commit('CURRENT_PLAY', timeCode)
break
case 2:
commit('CURRENT_PAUSE', timeCode)
break
}
dispatch('rtc/broadcast', { message: state.roomStatus, type: 'status' }, { root: true })
}
}
@@ -39,8 +77,58 @@ const mutations = {
SET_ADMIN (state) {
state.admin = true
},
BROADCAST_ROOMSTATUS (state) {
ADD_VOTE (state, { link, voterName }) {
var play = state.roomStatus.playlist.find(play => play.link === link)
if (play === undefined) {
play = {
link: link,
votes: 1,
voters: [
voterName
]
}
if (state.roomStatus.current.votes === 0) state.roomStatus.current = play
else state.roomStatus.playlist.push(play)
} else {
play.votes++
play.voters.push(voterName)
}
},
REMOVE_VOTE (state, { link, voterName }) {
var play = state.roomStatus.playlist.find(play => play.link === link)
play.votes--
const index = play.voters.indexOf(voterName)
if (index > -1) {
play.voters.splice(index, 1)
}
if (play.vote === 0) {
const index = state.roomStatus.playlist.indexOf(play)
if (index > -1) {
state.roomStatus.playlist.splice(index, 1)
}
}
},
CURRENT_END (state) {
if (state.roomStatus.playlist.length === 0) {
state.roomStatus.current.link = ''
state.roomStatus.current.title = ''
state.roomStatus.current.votes = 0
state.roomStatus.current.voters = []
} else {
state.roomStatus.playlist.sort((a, b) => {
return b.votes - a.votes
})
state.roomStatus.current = state.roomStatus.playlist.shift()
}
},
CURRENT_PAUSE (state, timeCode) {
state.roomStatus.player.playing = false
state.roomStatus.player.timeCode = timeCode
},
CURRENT_PLAY (state, timeCode) {
state.roomStatus.player.playing = true
state.roomStatus.player.timeCode = timeCode
}
}

View File

@@ -39,8 +39,8 @@ const actions = {
leave ({ commit }) {
commit('LEAVE')
},
broadcast ({ commit }, message) {
commit('BROADCAST', message)
broadcast ({ commit }, { message, type }) {
commit('BROADCAST', { message: message, type: type })
}
}
@@ -79,6 +79,7 @@ const mutations = {
var peer = state.peers.find(peer => peer.name === target)
peer.dataChannel = peer.connection.createDataChannel('dataChannel')
peer.dataChannel.onmessage = handleDataChannelMessage
peer.dataChannel.onopen = handleDataChannelStateChangeEvent
peer.dataChannel.onclose = handleDataChannelStateChangeEvent
@@ -127,9 +128,14 @@ const mutations = {
})
state.peers = {}
},
BROADCAST (state, message) {
BROADCAST (state, { message, type }) {
const data = JSON.stringify({
type: type,
message: message
})
console.log('[RTC] broadcast message ' + data)
state.peers.forEach(peer => {
peer.dataChannel.send(message)
peer.dataChannel.send(data)
})
}
}
@@ -167,17 +173,19 @@ function handleDataChannelCallback (event) {
peer.dataChannel.onopen = handleDataChannelStateChangeEvent
peer.dataChannel.onclose = handleDataChannelStateChangeEvent
store.dispatch('rtc/broadcast', store.state.room.roomStatus)
store.dispatch('rtc/broadcast', { message: store.state.room.roomStatus, type: 'status' })
}
function handleDataChannelMessage (event) {
console.log('[RTC] data channel message ' + event.data)
var data = event.data
var data = JSON.parse(event.data)
console.log('[RTC] data channel message type ' + data.type)
switch (data.type) {
case 'status':
store.dispatch('room/setRoomStatus', data.message)
break
case 'vote':
store.dispatch('room/vote', { link: data.message.link, isPositive: data.message.isPositive, voterName: data.message.voterName })
break
}
}