| 1 |
import { createBoard, playMove } from "./connect4.js"; |
| 2 |
|
| 3 |
function getWebSocketServer() { |
| 4 |
if (window.location.host === "python-websockets.github.io") { |
| 5 |
return "wss://websockets-tutorial.koyeb.app/"; |
| 6 |
} else if (window.location.host === "localhost:8000") { |
| 7 |
return "ws://localhost:8001/"; |
| 8 |
} else { |
| 9 |
throw new Error(`Unsupported host: ${window.location.host}`); |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
function initGame(websocket) { |
| 14 |
websocket.addEventListener("open", () => { |
| 15 |
// Send an "init" event according to who is connecting. |
| 16 |
const params = new URLSearchParams(window.location.search); |
| 17 |
let event = { type: "init" }; |
| 18 |
if (params.has("join")) { |
| 19 |
// Second player joins an existing game. |
| 20 |
event.join = params.get("join"); |
| 21 |
} else if (params.has("watch")) { |
| 22 |
// Spectator watches an existing game. |
| 23 |
event.watch = params.get("watch"); |
| 24 |
} else { |
| 25 |
// First player starts a new game. |
| 26 |
} |
| 27 |
websocket.send(JSON.stringify(event)); |
| 28 |
}); |
| 29 |
} |
| 30 |
|
| 31 |
function showMessage(message) { |
| 32 |
window.setTimeout(() => window.alert(message), 50); |
| 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. |
| 64 |
const params = new URLSearchParams(window.location.search); |
| 65 |
if (params.has("watch")) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// When clicking a column, send a "play" event for a move in that column. |
| 70 |
board.addEventListener("click", ({ target }) => { |
| 71 |
const column = target.dataset.column; |
| 72 |
// Ignore clicks outside a column. |
| 73 |
if (column === undefined) { |
| 74 |
return; |
| 75 |
} |
| 76 |
const event = { |
| 77 |
type: "play", |
| 78 |
column: parseInt(column, 10), |
| 79 |
}; |
| 80 |
websocket.send(JSON.stringify(event)); |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
window.addEventListener("DOMContentLoaded", () => { |
| 85 |
// Initialize the UI. |
| 86 |
const board = document.querySelector(".board"); |
| 87 |
createBoard(board); |
| 88 |
// Open the WebSocket connection and register event handlers. |
| 89 |
const websocket = new WebSocket(getWebSocketServer()); |
| 90 |
initGame(websocket); |
| 91 |
receiveMoves(board, websocket); |
| 92 |
sendMoves(board, websocket); |
| 93 |
}); |