Donnie Plumly
Donnie Plumly

Reputation: 11

WebRTC video chat end call button

I have a video chat app I am trying to add UI controls to. I am having issues with the end call button. I can end the call, but can't figure out how to get the video box to be removed by the button in the last line. If I close out of the call by changing URL or close the tab it will remove the video box.

Client script

const socket = io('/')
const videoGrid = document.getElementById('video-grid')
const myPeer = new Peer(undefined, {
  host: '/',
  port: '3001'
})

// Creating the <video> tag and muting the audio of your own feed
const myVideo = document.createElement('video')
myVideo.muted = true


const peers = {}
navigator.mediaDevices.getUserMedia({
  video: true,
  audio: true
}).then(stream => {
  addVideoStream(myVideo, stream)

  // answering the call and sending your stream
  myPeer.on('call', call => {
    call.answer(stream)
    const video = document.createElement('video')
    call.on('stream', userVideoStream => {
      addVideoStream(video, userVideoStream)
    })
  })

  socket.on('user-connected', userId => {
    connectToNewUser(userId, stream)
  })
})

socket.on('user-disconnected', userId => {
  if (peers[userId]) peers[userId].close()
})

myPeer.on('open', id => {
  socket.emit('join-room', ROOM_ID, id)
})
// connect to new users and adding their video
function connectToNewUser(userId, stream) {
  const call = myPeer.call(userId, stream)
  const video = document.createElement('video')
  call.on('stream', userVideoStream => {
    addVideoStream(video, userVideoStream)
  })

  // removes video from grid once they disconnect
  call.on('close', () => {
    video.remove()
  })
  peers[userId] = call
}

// connect your video and append to grid
function addVideoStream(video, stream) {
  video.srcObject = stream
  video.addEventListener('loadedmetadata', () => {
    video.play()
  })
  videoGrid.append(video)
}

// button to end call
let endCall = () => myVideo.srcObject.getTracks().forEach(track => track.stop())

Server script

const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)
const { v4: uuidV4 } = require('uuid')

app.set('view engine', 'ejs')
app.use(express.static('public'))

app.get('/', (req, res) => {
  res.redirect(`/${uuidV4()}`)
})

app.get('/:room', (req, res) => {
  res.render('room', { roomId: req.params.room })
})

io.on('connection', socket => {
  socket.on('join-room', (roomId, userId) => {
    socket.join(roomId)
    socket.to(roomId).broadcast.emit('user-connected', userId)

    socket.on('disconnect', () => {
      socket.to(roomId).broadcast.emit('user-disconnected', userId)
    })
  })
})

server.listen(3000)

Upvotes: 0

Views: 1241

Answers (1)

Donnie Plumly
Donnie Plumly

Reputation: 11

For anyone else that may run into this problem my solve was to make a html page that the end call button directed the user to when clicked. This way the URL would change taking the video stream away immediately solving the issue.

Upvotes: 1

Related Questions