Skip to content

Commit c384679

Browse files
committed
allow users to join any room by suffixing URL with /room-id
simple solution: snapdrop.net/ -> room by IP snapdrop.net/foo -> room foo invalidates a previously faulty PR: #288 and provides an alternative: specify the `snapdrop_mount` if you app is deployed on a different URL than `/`.
1 parent eac7800 commit c384679

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

client/scripts/network.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
window.URL = window.URL || window.webkitURL;
22
window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
3+
window.snapdrop_mount = "/"; // change this if app is deployed on a different path than `/`.
34

45
class ServerConnection {
56

@@ -58,7 +59,9 @@ class ServerConnection {
5859
// hack to detect if deployment or development environment
5960
const protocol = location.protocol.startsWith('https') ? 'wss' : 'ws';
6061
const webrtc = window.isRtcSupported ? '/webrtc' : '/fallback';
61-
const url = protocol + '://' + location.host + location.pathname + 'server' + webrtc;
62+
let room = location.pathname.replace(window.snapdrop_mount, "");
63+
room = room ? `?room=${room}` : "";
64+
const url = protocol + '://' + location.host + window.snapdrop_mount + 'server' + webrtc + room;
6265
return url;
6366
}
6467

docker/nginx/default.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ server {
1010
location / {
1111
root /usr/share/nginx/html;
1212
index index.html index.htm;
13+
try_files /$uri $uri /index.html /index.htm;
1314
}
1415

1516
location /server {
@@ -18,6 +19,7 @@ server {
1819
proxy_set_header Connection "upgrade";
1920
proxy_set_header Upgrade $http_upgrade;
2021
proxy_set_header X-Forwarded-for $remote_addr;
22+
proxy_set_header X-Room $arg_room;
2123
}
2224

2325
location /ca.crt {
@@ -72,4 +74,3 @@ server {
7274
root /usr/share/nginx/html;
7375
}
7476
}
75-

server/index.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)