-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow socket connection without authentication (#15)
- Loading branch information
1 parent
274bb82
commit b9f1d82
Showing
8 changed files
with
102 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,55 @@ | ||
import { LenraApp, Listener } from '@lenra/client'; | ||
|
||
const app = new LenraApp({ | ||
appName: "XXX-XXX-XXX", | ||
clientId: "XXX-XXX-XXX", | ||
}); | ||
|
||
let authenticated = false; | ||
|
||
console.log("connecting..."); | ||
app.connect().then(() => { | ||
app.openSocket().then(appConnected); | ||
|
||
const authenticationButton = document.querySelector("body > button"); | ||
authenticationButton.onclick = () => { | ||
app.disconnect(); | ||
authenticated = true; | ||
app.connect().then(appConnected); | ||
}; | ||
const disconnectButton = document.createElement("button"); | ||
disconnectButton.textContent = "Disconnect"; | ||
disconnectButton.onclick = () => { | ||
app.disconnect(); | ||
disconnectButton.style.display = "none"; | ||
}; | ||
document.body.appendChild(disconnectButton); | ||
|
||
function appConnected() { | ||
console.log("Connected !"); | ||
|
||
const disconnectButton = document.createElement("button"); | ||
disconnectButton.textContent = "Disconnect"; | ||
disconnectButton.onclick = () => { | ||
app.disconnect(); | ||
disconnectButton.remove(); | ||
}; | ||
document.body.appendChild(disconnectButton); | ||
disconnectButton.style.display = undefined; | ||
|
||
const counters = document.querySelectorAll(".counter"); | ||
|
||
counters.forEach((counter) => { | ||
// Filter counters that need authentication | ||
if (!authenticated && counter.classList.contains("authenticated")) return; | ||
|
||
const button = counter.querySelector("button"); | ||
const output = counter.querySelector("output"); | ||
app.route(`/counter/${counter.id}`, | ||
console.log("Open route for counter", counter.id); | ||
app.route(`/counter/${counter.id}`, | ||
/** | ||
* @param {{value: number, onIncrement: Listener<{value: string}>}} data | ||
*/ | ||
(data) => { | ||
output.textContent = data.value; | ||
button.onclick = () => { | ||
output.classList.add("loading"); | ||
data.onIncrement({value: "custom value"}).then(() => { | ||
output.classList.remove("loading"); | ||
}); | ||
}; | ||
}); | ||
output.textContent = data.value; | ||
button.onclick = () => { | ||
output.classList.add("loading"); | ||
data.onIncrement({ value: "custom value" }).then(() => { | ||
output.classList.remove("loading"); | ||
}); | ||
}; | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters