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