127.0.0.1:8000 watch-together / master server / websocket / messages / stop-watching.js
master

Tree @master (Download .tar.gz)

stop-watching.js @masterraw · history · blame

const watchrooms = require('../../utils/watchrooms');
const broadcast = require('../broadcast');

module.exports = function(user_id, video_id) {
    // set video_id in case it was undefined
    video_id = watchrooms.leave(user_id, video_id);
    if (video_id) {
        let watchroom = watchrooms.get(video_id);
        if (watchroom) {
            broadcast({
                command: "announce",
                data: {
                    target: "watchroom",
                    type: "user-left",
                    user_id: user_id
                }
            }, watchroom.viewers);
        } else {
            broadcast({
                command: "announce",
                data: {
                    target: "all",
                    type: "watchroom-closed",
                    video_id: video_id
                }
            });
        }
    }
};