@@ -11,7 +11,7 @@ app.use(express.static(path.join(__dirname, 'www')));
11
11
// Get PORT from env variable else assign 3000 for development
12
12
const PORT = process . env . PORT || 3000 ;
13
13
server . listen ( PORT , null , function ( ) {
14
- console . log ( " Listening on port " + PORT ) ;
14
+ console . log ( ' Listening on port ' + PORT ) ;
15
15
} ) ;
16
16
17
17
// All URL patterns should served with the same file.
@@ -20,22 +20,24 @@ app.get(['/', '/:room'], (req, res) => res.sendFile(path.join(__dirname, 'www/in
20
20
const channels = { } ;
21
21
const sockets = { } ;
22
22
23
- io . sockets . on ( 'connection' , ( socket ) => {
23
+ io . sockets . on ( 'connection' , socket => {
24
+ const socketHostName = socket . handshake . headers . host . split ( ':' ) [ 0 ] ;
25
+
24
26
socket . channels = { } ;
25
27
sockets [ socket . id ] = socket ;
26
28
27
- console . log ( "[" + socket . id + " ] connection accepted" ) ;
29
+ console . log ( '[' + socket . id + ' ] connection accepted' ) ;
28
30
socket . on ( 'disconnect' , ( ) => {
29
31
for ( const channel in socket . channels ) {
30
32
part ( channel ) ;
31
33
}
32
- console . log ( "[" + socket . id + " ] disconnected" ) ;
34
+ console . log ( '[' + socket . id + ' ] disconnected' ) ;
33
35
delete sockets [ socket . id ] ;
34
36
} ) ;
35
37
36
- socket . on ( 'join' , ( config ) => {
37
- console . log ( "[" + socket . id + " ] join " , config ) ;
38
- const channel = config . channel ;
38
+ socket . on ( 'join' , config => {
39
+ console . log ( '[' + socket . id + ' ] join ' , config ) ;
40
+ const channel = socketHostName + config . channel ;
39
41
40
42
// Already Joined
41
43
if ( channel in socket . channels ) return ;
@@ -45,44 +47,50 @@ io.sockets.on('connection', (socket) => {
45
47
}
46
48
47
49
for ( id in channels [ channel ] ) {
48
- channels [ channel ] [ id ] . emit ( 'addPeer' , { ' peer_id' : socket . id , ' should_create_offer' : false } ) ;
49
- socket . emit ( 'addPeer' , { ' peer_id' : id , ' should_create_offer' : true } ) ;
50
+ channels [ channel ] [ id ] . emit ( 'addPeer' , { peer_id : socket . id , should_create_offer : false } ) ;
51
+ socket . emit ( 'addPeer' , { peer_id : id , should_create_offer : true } ) ;
50
52
}
51
53
52
54
channels [ channel ] [ socket . id ] = socket ;
53
55
socket . channels [ channel ] = channel ;
54
56
} ) ;
55
57
56
- const part = ( channel ) => {
58
+ const part = channel => {
57
59
// Socket not in channel
58
60
if ( ! ( channel in socket . channels ) ) return ;
59
61
60
62
delete socket . channels [ channel ] ;
61
63
delete channels [ channel ] [ socket . id ] ;
62
64
63
65
for ( id in channels [ channel ] ) {
64
- channels [ channel ] [ id ] . emit ( 'removePeer' , { ' peer_id' : socket . id } ) ;
65
- socket . emit ( 'removePeer' , { ' peer_id' : id } ) ;
66
+ channels [ channel ] [ id ] . emit ( 'removePeer' , { peer_id : socket . id } ) ;
67
+ socket . emit ( 'removePeer' , { peer_id : id } ) ;
66
68
}
67
- }
69
+ } ;
68
70
69
- socket . on ( 'relayICECandidate' , ( config ) => {
71
+ socket . on ( 'relayICECandidate' , config => {
70
72
let peer_id = config . peer_id ;
71
73
let ice_candidate = config . ice_candidate ;
72
- console . log ( "[" + socket . id + " ] relay ICE-candidate to [" + peer_id + "] " , ice_candidate ) ;
74
+ console . log ( '[' + socket . id + ' ] relay ICE-candidate to [' + peer_id + '] ' , ice_candidate ) ;
73
75
74
76
if ( peer_id in sockets ) {
75
- sockets [ peer_id ] . emit ( 'iceCandidate' , { ' peer_id' : socket . id , ' ice_candidate' : ice_candidate } ) ;
77
+ sockets [ peer_id ] . emit ( 'iceCandidate' , { peer_id : socket . id , ice_candidate : ice_candidate } ) ;
76
78
}
77
79
} ) ;
78
80
79
- socket . on ( 'relaySessionDescription' , ( config ) => {
81
+ socket . on ( 'relaySessionDescription' , config => {
80
82
let peer_id = config . peer_id ;
81
83
let session_description = config . session_description ;
82
- console . log ( "[" + socket . id + "] relay SessionDescription to [" + peer_id + "] " , session_description ) ;
84
+ console . log (
85
+ '[' + socket . id + '] relay SessionDescription to [' + peer_id + '] ' ,
86
+ session_description
87
+ ) ;
83
88
84
89
if ( peer_id in sockets ) {
85
- sockets [ peer_id ] . emit ( 'sessionDescription' , { 'peer_id' : socket . id , 'session_description' : session_description } ) ;
90
+ sockets [ peer_id ] . emit ( 'sessionDescription' , {
91
+ peer_id : socket . id ,
92
+ session_description : session_description
93
+ } ) ;
86
94
}
87
95
} ) ;
88
- } ) ;
96
+ } ) ;
0 commit comments