@@ -67,9 +67,9 @@ class SnapdropServer {
6767 }
6868
6969 // relay message to recipient
70- if ( message . to && this . _rooms [ sender . ip ] ) {
70+ if ( message . to && this . _rooms [ sender . room ] ) {
7171 const recipientId = message . to ; // TODO: sanitize
72- const recipient = this . _rooms [ sender . ip ] [ recipientId ] ;
72+ const recipient = this . _rooms [ sender . room ] [ recipientId ] ;
7373 delete message . to ;
7474 // add sender id
7575 message . sender = sender . id ;
@@ -80,13 +80,13 @@ class SnapdropServer {
8080
8181 _joinRoom ( peer ) {
8282 // if room doesn't exist, create it
83- if ( ! this . _rooms [ peer . ip ] ) {
84- this . _rooms [ peer . ip ] = { } ;
83+ if ( ! this . _rooms [ peer . room ] ) {
84+ this . _rooms [ peer . room ] = { } ;
8585 }
8686
8787 // notify all other peers
88- for ( const otherPeerId in this . _rooms [ peer . ip ] ) {
89- const otherPeer = this . _rooms [ peer . ip ] [ otherPeerId ] ;
88+ for ( const otherPeerId in this . _rooms [ peer . room ] ) {
89+ const otherPeer = this . _rooms [ peer . room ] [ otherPeerId ] ;
9090 this . _send ( otherPeer , {
9191 type : 'peer-joined' ,
9292 peer : peer . getInfo ( )
@@ -95,8 +95,8 @@ class SnapdropServer {
9595
9696 // notify peer about the other peers
9797 const otherPeers = [ ] ;
98- for ( const otherPeerId in this . _rooms [ peer . ip ] ) {
99- otherPeers . push ( this . _rooms [ peer . ip ] [ otherPeerId ] . getInfo ( ) ) ;
98+ for ( const otherPeerId in this . _rooms [ peer . room ] ) {
99+ otherPeers . push ( this . _rooms [ peer . room ] [ otherPeerId ] . getInfo ( ) ) ;
100100 }
101101
102102 this . _send ( peer , {
@@ -105,24 +105,24 @@ class SnapdropServer {
105105 } ) ;
106106
107107 // add peer to room
108- this . _rooms [ peer . ip ] [ peer . id ] = peer ;
108+ this . _rooms [ peer . room ] [ peer . id ] = peer ;
109109 }
110110
111111 _leaveRoom ( peer ) {
112- if ( ! this . _rooms [ peer . ip ] || ! this . _rooms [ peer . ip ] [ peer . id ] ) return ;
113- this . _cancelKeepAlive ( this . _rooms [ peer . ip ] [ peer . id ] ) ;
112+ if ( ! this . _rooms [ peer . room ] || ! this . _rooms [ peer . room ] [ peer . id ] ) return ;
113+ this . _cancelKeepAlive ( this . _rooms [ peer . room ] [ peer . id ] ) ;
114114
115115 // delete the peer
116- delete this . _rooms [ peer . ip ] [ peer . id ] ;
116+ delete this . _rooms [ peer . room ] [ peer . id ] ;
117117
118118 peer . socket . terminate ( ) ;
119119 //if room is empty, delete the room
120- if ( ! Object . keys ( this . _rooms [ peer . ip ] ) . length ) {
121- delete this . _rooms [ peer . ip ] ;
120+ if ( ! Object . keys ( this . _rooms [ peer . room ] ) . length ) {
121+ delete this . _rooms [ peer . room ] ;
122122 } else {
123123 // notify all other peers
124- for ( const otherPeerId in this . _rooms [ peer . ip ] ) {
125- const otherPeer = this . _rooms [ peer . ip ] [ otherPeerId ] ;
124+ for ( const otherPeerId in this . _rooms [ peer . room ] ) {
125+ const otherPeer = this . _rooms [ peer . room ] [ otherPeerId ] ;
126126 this . _send ( otherPeer , { type : 'peer-left' , peerId : peer . id } ) ;
127127 }
128128 }
@@ -169,6 +169,7 @@ class Peer {
169169
170170 // set remote ip
171171 this . _setIP ( request ) ;
172+ this . _setRoom ( request )
172173
173174 // set peer id
174175 this . _setPeerId ( request )
@@ -193,6 +194,14 @@ class Peer {
193194 }
194195 }
195196
197+ _setRoom ( request ) {
198+ if ( request . headers [ 'x-room' ] ) {
199+ this . room = request . headers [ 'x-room' ] ;
200+ } else {
201+ this . room = this . ip ;
202+ }
203+ }
204+
196205 _setPeerId ( request ) {
197206 if ( request . peerId ) {
198207 this . id = request . peerId ;
@@ -202,7 +211,7 @@ class Peer {
202211 }
203212
204213 toString ( ) {
205- return `<Peer id=${ this . id } ip=${ this . ip } rtcSupported=${ this . rtcSupported } >`
214+ return `<Peer id=${ this . id } ip=${ this . ip } room= ${ this . room } rtcSupported=${ this . rtcSupported } >`
206215 }
207216
208217 _setName ( req ) {
0 commit comments