framez-server/Server/FrameLinker/FrameLinker.js

45 lines
1.3 KiB
JavaScript

const Logger = require('../Utils/Logger')
const frameViewers = {}
const TAG = '[FrameLinker]';
module.exports = {
init: (io) => {
io.sockets.on('connection', (socket) => {
let key = null
Logger.log(TAG, 'New connection from client: ', socket.id)
socket.on('disconnect', () => {
Logger.log(TAG, 'Frame Viewer disconnected disconnected')
if(key){
delete frameViewers[key];
}
});
socket.on('register_key', (data) => {
if(frameViewers[key]){
Logger.warn(TAG, 'Key in use - sending register_fail')
socket.emit('register_fail')
return;
}
Logger.debug(TAG, 'Registering key ' + data.key)
key = data.key
frameViewers[key] = socket
Logger.debug(TAG, 'is key', key, 'valid:', frameViewers.hasOwnProperty(key))
})
});
},
isKeyValid: (key)=>{
Logger.debug(TAG, 'is key', key, 'valid:', frameViewers.hasOwnProperty(key))
if(frameViewers.hasOwnProperty(key)) return true;
return false;
},
linkFrame: (frameId, accessToken, key)=>{
return new Promise((resolve, reject) => {
if(!frameViewers[key]) reject('No Frame Viewer Found')
else{
frameViewers[key].emit('consume_frame', {frame_id: frameId, accessToken: accessToken})
resolve()
}
});
}
};