]> git.r.bdr.sh - rbdr/junction/blobdiff - server/events.js
Refactor server code
[rbdr/junction] / server / events.js
index 062b7c1e7989dfc379ef5b5386c8799d56c3ce74..bad9046f2f14fff4313b1af16d797a46e9b6e7a8 100644 (file)
@@ -1,41 +1,56 @@
-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,
+  });
 }
 
 }
 
-function removePeer(peerId) {
-    return {
-        peer_id: peerId,
-    }
+export function removePeer(peerId) {
+  return internals.emitter.bind(null, types.REMOVE_PEER, {
+    peerId,
+  });
 }
 
 }
 
-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,
+  });
 }
 
 }
 
-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,
+  });
 }
 
 }
 
-module.exports = {
-    types: {
-        ADD_PEER: 'addPeer',
-        REMOVE_PEER: 'removePeer',
-        RELAY_SESSION_DESCRIPTION: 'relaySessionDescription',
-        RELAY_ICE_CANDIDATE: 'relayICECandidate',
-        ICE_CANDIDATE_RECEIVED: 'ICECandidateReceived',
-        SESSION_DESCRIPTION_RECEIVED: 'SessionDescriptionReceived',
-    },
-    addPeer,
-    removePeer,
-    SessionDescriptionReceived: SessionDescriptionReceived,
-    ICECandidateReceived: ICECandidateReceived,
-}
\ No newline at end of file
+export function answerReceived(peerId, answer) {
+  return internals.emitter.bind(null, types.ANSWER_RECEIVED, {
+    peerId,
+    answer,
+  });
+}