1+ const { EventEmitter } = require ( "ws" ) ;
2+ const globalUtils = require ( "../helpers/globalutils" ) ;
3+ const session = require ( "../helpers/session" ) ;
4+
5+ const OPCODES = {
6+ IDENTIFY : "IDENTIFY" ,
7+ ALRIGHT : "ALRIGHT" ,
8+ HEARTBEAT_INFO : "HEARTBEAT_INFO" ,
9+ ANSWER : "ANSWER" ,
10+ VIDEO_BATCH : "VIDEO_BATCH" ,
11+ SPEAKING_BATCH : "SPEAKING_BATCH" ,
12+ HEARTBEAT : "HEARTBEAT" ,
13+ HEARTBEAT_ACK : "HEARTBEAT_ACK" ,
14+ } ;
15+
16+ async function handleIdentify ( socket , packet ) {
17+ let public_ip = packet . d . public_ip ;
18+ let public_port = packet . d . public_port ;
19+ let timestamp = packet . d . timestamp ;
20+
21+ global . mrServer . debug ( `New media server has connected! Added to internal store.` ) ;
22+
23+ //to-do find a proper & fast way to lookup these public ips to serve whats close to a user
24+
25+ socket . public_ip = public_ip ;
26+ socket . public_port = public_port ;
27+ socket . emitter = new EventEmitter ( ) ;
28+
29+ global . mrServer . servers . set ( public_ip , {
30+ socket : socket ,
31+ port : public_port ,
32+ seen_at : timestamp
33+ } ) ;
34+
35+ socket . send ( JSON . stringify ( {
36+ op : OPCODES . ALRIGHT ,
37+ d : {
38+ location : global . mrServer . servers . size + 1
39+ }
40+ } ) ) ;
41+ }
42+
43+ async function handleHeartbeat ( socket , packet ) {
44+ if ( ! socket . hb ) return ;
45+
46+ socket . hb . acknowledge ( packet . d ) ;
47+ socket . hb . reset ( ) ;
48+ }
49+
50+ async function handleAnswer ( socket , packet ) {
51+ socket . emitter . emit ( 'answer-received' , packet . d ) ;
52+ }
53+
54+ async function handleVideoBatch ( socket , packet ) {
55+ socket . emitter . emit ( 'video-batch' , packet . d ) ;
56+ }
57+
58+
59+ async function handleSpeakingBatch ( socket , packet ) {
60+ socket . emitter . emit ( 'speaking-batch' , packet . d ) ;
61+ }
62+
63+ const mrHandlers = {
64+ [ OPCODES . IDENTIFY ] : handleIdentify ,
65+ [ OPCODES . HEARTBEAT ] : handleHeartbeat ,
66+ [ OPCODES . ANSWER ] : handleAnswer ,
67+ [ OPCODES . VIDEO_BATCH ] : handleVideoBatch ,
68+ [ OPCODES . SPEAKING_BATCH ] : handleSpeakingBatch
69+ } ;
70+
71+ module . exports = {
72+ mrHandlers,
73+ OPCODES
74+ } ;
0 commit comments