X-Git-Url: https://git.r.bdr.sh/rbdr/junction/blobdiff_plain/80172072ed99b14e3d5b435b097888a4621f78bc..ec4adc33ec00dfe4a5720a13551428aaddaf72d9:/server/events.js diff --git a/server/events.js b/server/events.js index 99e6b37..bad9046 100644 --- a/server/events.js +++ b/server/events.js @@ -1,35 +1,56 @@ -export function addPeer(peerId, shouldCreateOffer) { - return { - peer_id: peerId, - should_create_offer: shouldCreateOffer, - } +const internals = { + emitter(name, data, socket) { + socket.emit(name, data); + console.debug(`[${name}] ${JSON.stringify(data)}`); + }, +}; + +export const types = { + /* Listened */ + CONNECTION: "connection", + JOIN: "join", + DISCONNECTING: "disconnecting", + RELAY_ICE_CANDIDATE: "relayICECandidate", + RELAY_OFFER: "relayOffer", + RELAY_ANSWER: "relayAnswer", + + /* Emitted */ + ADD_PEER: "addPeer", + REMOVE_PEER: "removePeer", + OFFER_RECEIVED: "offerReceived", + ANSWER_RECEIVED: "answerReceived", + ICE_CANDIDATE_RECEIVED: "ICECandidateReceived", +}; + +export function addPeer(peerId) { + return internals.emitter.bind(null, types.ADD_PEER, { + peerId, + }); } export function removePeer(peerId) { - return { - peer_id: peerId, - } + return internals.emitter.bind(null, types.REMOVE_PEER, { + peerId, + }); } -export function SessionDescriptionReceived(peerId, sessionDescription) { - return { - peer_id: peerId, - session_description: sessionDescription, - } +export function ICECandidateReceived(peerId, candidate) { + return internals.emitter.bind(null, types.ICE_CANDIDATE_RECEIVED, { + peerId, + candidate, + }); } -export function ICECandidateReceived(peerId, ICECandidate) { - return { - peer_id: peerId, - ice_candidate: ICECandidate, - } +export function offerReceived(peerId, offer) { + return internals.emitter.bind(null, types.OFFER_RECEIVED, { + peerId, + offer, + }); } -export const types = { - ADD_PEER: 'addPeer', - REMOVE_PEER: 'removePeer', - RELAY_SESSION_DESCRIPTION: 'relaySessionDescription', - RELAY_ICE_CANDIDATE: 'relayICECandidate', - ICE_CANDIDATE_RECEIVED: 'ICECandidateReceived', - SESSION_DESCRIPTION_RECEIVED: 'SessionDescriptionReceived', -}; +export function answerReceived(peerId, answer) { + return internals.emitter.bind(null, types.ANSWER_RECEIVED, { + peerId, + answer, + }); +}