(board, websocket)
| 23 | } |
| 24 | |
| 25 | function receiveMoves(board, websocket) { |
| 26 | websocket.addEventListener("message", ({ data }) => { |
| 27 | const event = JSON.parse(data); |
| 28 | switch (event.type) { |
| 29 | case "init": |
| 30 | // Create links for inviting the second player and spectators. |
| 31 | document.querySelector(".join").href = "?join=" + event.join; |
| 32 | document.querySelector(".watch").href = "?watch=" + event.watch; |
| 33 | break; |
| 34 | case "play": |
| 35 | // Update the UI with the move. |
| 36 | playMove(board, event.player, event.column, event.row); |
| 37 | break; |
| 38 | case "win": |
| 39 | showMessage(`Player ${event.player} wins!`); |
| 40 | // No further messages are expected; close the WebSocket connection. |
| 41 | websocket.close(1000); |
| 42 | break; |
| 43 | case "error": |
| 44 | showMessage(event.message); |
| 45 | break; |
| 46 | default: |
| 47 | throw new Error(`Unsupported event type: ${event.type}.`); |
| 48 | } |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | function sendMoves(board, websocket) { |
| 53 | // Don't send moves for a spectator watching a game. |
no test coverage detected
searching dependent graphs…