Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
ksk_ef:web:apps:server [2024-08-11 11:04] – hof | ksk_ef:web:apps:server [2024-08-11 11:20] (aktuell) – hof | ||
---|---|---|---|
Zeile 111: | Zeile 111: | ||
<code javascript app.js> | <code javascript app.js> | ||
import {TicTacToe} from ' | import {TicTacToe} from ' | ||
+ | |||
+ | let the_game = new TicTacToe(); | ||
+ | |||
+ | app.get('/ | ||
+ | return the_game.toJson(); | ||
+ | }) | ||
+ | |||
+ | app.get('/ | ||
+ | the_game.set(req.params[' | ||
+ | res.json(the_game.toJson()) | ||
+ | }) | ||
+ | </ | ||
+ | |||
+ | Nun sollte unser Server ein einziges Game betreiben. Der Zustand sollte unter http:// | ||
+ | |||
+ | Was gibt es noch zu lösen? | ||
+ | * Der Browser benötigt auch Javascript. Der Click-Handler jedes Buttons soll das `set` API aufrufen und anschliessend den Inhalt des HTMLs an den Spielzustand anpassen. | ||
+ | * Wie weiss der Browser, ob er `X` oder `O` ist? | ||
+ | * Wie können mehrere Spiele betreut werden (Hinweis: schau dir die `gameid` oben an...!) | ||
+ | |||
+ | ### Client-Side Javascript | ||
+ | Der Browser benötigt auch Javascript. Der Click-Handler jedes Buttons soll das `set` API aufrufen und anschliessend den Inhalt des HTMLs an den Spielzustand anpassen. | ||
+ | |||
+ | <code javascript tictactoe_client.js> | ||
+ | class Tictactoe_Client { | ||
+ | | ||
+ | /** | ||
+ | * Fetches the game state, updates the UI, and installs click handlers. | ||
+ | */ | ||
+ | async init() { | ||
+ | let i = 0; | ||
+ | for (const button of this.view.grid.getElementsByTagName(" | ||
+ | const cell = i; | ||
+ | button.addEventListener(' | ||
+ | this.handleJsonUrl(`/ | ||
+ | }); | ||
+ | i++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Fetches the given URL and updates the internal state from the parsed JSON. | ||
+ | | ||
+ | * Keeps polling for updates if the updated state could change remotely. | ||
+ | | ||
+ | * @param {string} url the URL to fetch that will return tictactoe JSON. | ||
+ | */ | ||
+ | async handleJsonUrl(url) { | ||
+ | let response = await fetch(url); | ||
+ | if (!response.ok) { | ||
+ | let error = await response.text(); | ||
+ | console.log(" | ||
+ | return; | ||
+ | } | ||
+ | const json = await response.json(); | ||
+ | this.updateHtml(json); | ||
+ | } | ||
+ | | ||
+ | /** Update the HTML based on JSON game state. */ | ||
+ | updateHtml(json) { | ||
+ | let i = 0; | ||
+ | |||
+ | for (const button of this.grid.getElementsByTagName(" | ||
+ | const cellText = json.grid[i]; | ||
+ | // Set the data-state attribute which drives CSS formatting. | ||
+ | button.setAttribute(' | ||
+ | // Set the text contents of the cell. | ||
+ | button.textContent = cellText; | ||
+ | i++; | ||
+ | } | ||
+ | } | ||
+ | } | ||
</ | </ | ||
Zeile 152: | Zeile 224: | ||
}) | }) | ||
</ | </ | ||
+ | |||
+ | ### Hinweise | ||
+ | |||
+ | * Die ganze Web-App mit JS & Node: https:// | ||
+ | * Alternative mit Python & Flask auf der Serverseite: |