@@ -67,9 +67,9 @@ class SnapdropServer {
67
67
}
68
68
69
69
// relay message to recipient
70
- if ( message . to && this . _rooms [ sender . ip ] ) {
70
+ if ( message . to && this . _rooms [ sender . room ] ) {
71
71
const recipientId = message . to ; // TODO: sanitize
72
- const recipient = this . _rooms [ sender . ip ] [ recipientId ] ;
72
+ const recipient = this . _rooms [ sender . room ] [ recipientId ] ;
73
73
delete message . to ;
74
74
// add sender id
75
75
message . sender = sender . id ;
@@ -80,13 +80,13 @@ class SnapdropServer {
80
80
81
81
_joinRoom ( peer ) {
82
82
// 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 ] = { } ;
85
85
}
86
86
87
87
// 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 ] ;
90
90
this . _send ( otherPeer , {
91
91
type : 'peer-joined' ,
92
92
peer : peer . getInfo ( )
@@ -95,8 +95,8 @@ class SnapdropServer {
95
95
96
96
// notify peer about the other peers
97
97
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 ( ) ) ;
100
100
}
101
101
102
102
this . _send ( peer , {
@@ -105,24 +105,24 @@ class SnapdropServer {
105
105
} ) ;
106
106
107
107
// add peer to room
108
- this . _rooms [ peer . ip ] [ peer . id ] = peer ;
108
+ this . _rooms [ peer . room ] [ peer . id ] = peer ;
109
109
}
110
110
111
111
_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 ] ) ;
114
114
115
115
// delete the peer
116
- delete this . _rooms [ peer . ip ] [ peer . id ] ;
116
+ delete this . _rooms [ peer . room ] [ peer . id ] ;
117
117
118
118
peer . socket . terminate ( ) ;
119
119
//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 ] ;
122
122
} else {
123
123
// 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 ] ;
126
126
this . _send ( otherPeer , { type : 'peer-left' , peerId : peer . id } ) ;
127
127
}
128
128
}
@@ -169,6 +169,7 @@ class Peer {
169
169
170
170
// set remote ip
171
171
this . _setIP ( request ) ;
172
+ this . _setRoom ( request )
172
173
173
174
// set peer id
174
175
this . _setPeerId ( request )
@@ -193,6 +194,14 @@ class Peer {
193
194
}
194
195
}
195
196
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
+
196
205
_setPeerId ( request ) {
197
206
if ( request . peerId ) {
198
207
this . id = request . peerId ;
@@ -202,7 +211,7 @@ class Peer {
202
211
}
203
212
204
213
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 } >`
206
215
}
207
216
208
217
_setName ( req ) {
0 commit comments