Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 85d734e

Browse files
niinpatelAlan Shaw
authored and
Alan Shaw
committed
docs: add rooms feature to examples/exchange-files (#1949)
As mentioned in issue #1632, currently, it is impossible to guess that there is the room feature on the exchange files example. So, I added a textbox and a button which can be used for subscribing to a different room. Here's a small preview of the feature: ![ipfs rooms demo (2)](https://user-images.githubusercontent.com/31539366/54750766-d7742400-4bfe-11e9-825c-7b0ff6007c8a.gif) closes #1632 License: MIT Signed-off-by: Nitin Patel <[email protected]>
1 parent 69f235f commit 85d734e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

exchange-files-in-browser/public/app.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ const $allDisabledButtons = document.querySelectorAll('button:disabled')
2424
const $allDisabledInputs = document.querySelectorAll('input:disabled')
2525
const $allDisabledElements = document.querySelectorAll('.disabled')
2626

27-
const FILES = []
28-
const workspace = location.hash
27+
// Workspace inputs
28+
const $workspaceInput = document.querySelector('#workspace-input')
29+
const $workspaceBtn = document.querySelector('#workspace-btn')
30+
31+
let FILES = []
32+
let workspace = location.hash
2933

3034
let fileSize = 0
3135

@@ -65,6 +69,8 @@ function start () {
6569
.catch((error) => onError(error))
6670

6771
subscribeToWorkpsace()
72+
73+
window.addEventListener('hashchange', workspaceUpdated)
6874
})
6975
}
7076
}
@@ -87,9 +93,25 @@ const messageHandler = (message) => {
8793

8894
const subscribeToWorkpsace = () => {
8995
node.pubsub.subscribe(workspace, messageHandler)
96+
.then(() => {
97+
const msg = `Subscribed to workspace ${workspace}`
98+
$logs.innerHTML = msg
99+
})
90100
.catch(() => onError('An error occurred when subscribing to the workspace.'))
91101
}
92102

103+
// unsubscribe from old workspace and re-subscribe to new one
104+
const workspaceUpdated = () => {
105+
node.pubsub.unsubscribe(workspace).then(() => {
106+
// clear files from old workspace
107+
FILES = []
108+
$fileHistory.innerHTML = ''
109+
110+
workspace = location.hash
111+
subscribeToWorkpsace()
112+
})
113+
}
114+
93115
const publishHash = (hash) => {
94116
const data = Buffer.from(hash)
95117

@@ -327,6 +349,9 @@ const startApplication = () => {
327349
$dragContainer.addEventListener('dragleave', onDragLeave)
328350
$fetchButton.addEventListener('click', getFile)
329351
$connectButton.addEventListener('click', connectToPeer)
352+
$workspaceBtn.addEventListener('click', () => {
353+
window.location.hash = $workspaceInput.value
354+
})
330355

331356
start()
332357
}

exchange-files-in-browser/public/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ <h3>Logs</h3>
3131
<pre id="logs" class="success">Initializing node...</pre>
3232
</div>
3333
</div>
34+
35+
<div>
36+
<h3>Workspace</h3>
37+
<div class="input-button">
38+
<input id="workspace-input" type="text" placeholder="Enter workspace name" disabled />
39+
<button id="workspace-btn" disabled>Join</button>
40+
</div>
41+
</div>
3442
</div>
3543

3644

0 commit comments

Comments
 (0)