Skip to content

Commit 0903526

Browse files
committed
Configure WebSocket server address.
1 parent ac96d65 commit 0903526

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { createBoard, playMove } from "./connect4.js";
22

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+
313
function initGame(websocket) {
414
websocket.addEventListener("open", () => {
515
// Send an "init" event according to who is connecting.
@@ -76,7 +86,7 @@ window.addEventListener("DOMContentLoaded", () => {
7686
const board = document.querySelector(".board");
7787
createBoard(board);
7888
// Open the WebSocket connection and register event handlers.
79-
const websocket = new WebSocket("ws://localhost:8001/");
89+
const websocket = new WebSocket(getWebSocketServer());
8090
initGame(websocket);
8191
receiveMoves(board, websocket);
8292
sendMoves(board, websocket);

0 commit comments

Comments
 (0)