1
0
mirror of https://github.com/thib8956/tic-tac-toe-ws.git synced 2026-03-24 14:37:56 +00:00

fix: improve npm scripts and fix localhost detection

This commit is contained in:
2026-03-21 10:03:59 +01:00
parent ae7c3a4662
commit cccd62b8ad
8 changed files with 24 additions and 32 deletions

View File

@@ -15,10 +15,7 @@ jobs:
- name: Install and Build - name: Install and Build
run: | run: |
npm ci npm ci
npx tsc npm run build
mkdir dist
mv *.js dist/
mv *.html dist/
- name: Setup Pages - name: Setup Pages
id: pages id: pages
uses: actions/configure-pages@v5 uses: actions/configure-pages@v5

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules/ node_modules/
*.js dist/

View File

@@ -1,21 +1,17 @@
# Tic tac toe websockets # Tic tac toe websockets
Two players tic-tac-joe implemented with an HTML canvas and websockets Two players tic-tac-joe implemented with an HTML canvas and websockets
![](screenshot.png) ![Screenshot](screenshot.png)
## How to run ## How to run
``` ```shell
// compilation of .mts files in watch mode $ npm run build:watch
$ tsc -w
// serve static files (localhost:8000) // serve static files (localhost:8000)
$ python serve.py $ npm run serve
// run server // run server
$ node server.mjs $ npm run start:watch
``` ```
Go to http://localhost:8000 Go to [](http://localhost:8000)

View File

@@ -4,10 +4,9 @@ const ANIMATE_DURATION = 500; // ms
const GRID_PADDING = 10; const GRID_PADDING = 10;
const MESSAGE_PADDING = 20; const MESSAGE_PADDING = 20;
let address = "ws://localhost:1234"; const address = ["127.0.0.1", "localhost"].includes(window.location.hostname)
if (window.location.hostname !== "localhost") { ? "ws://localhost:1234"
address = "wss://tic-tac-toe-ws-production.up.railway.app"; : "wss://tic-tac-toe-ws-production.up.railway.app";
}
const ws = new WebSocket(address); const ws = new WebSocket(address);
interface Point { interface Point {

View File

@@ -1,5 +1,3 @@
export type MessageKind = "click" | "hello" | "update" | "endgame" | "reset" | "spectate";
export type Message = export type Message =
| { kind: "click", data: Click } | { kind: "click", data: Click }
| { kind: "hello", data: Hello } | { kind: "hello", data: Hello }

View File

@@ -5,8 +5,11 @@
"main": "index.js", "main": "index.js",
"type": "module", "type": "module",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc && cp index.html dist/",
"start": "node server.js" "build:watch": "tsc --watch && cp index.html dist/",
"start": "node dist/server.js",
"start:watch": "node --watch dist/server.js",
"serve": "python serve.py"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

View File

@@ -1,11 +1,7 @@
import http.server import http.server
from functools import partial
HandlerClass = http.server.SimpleHTTPRequestHandler HandlerClass = partial(http.server.SimpleHTTPRequestHandler, directory="dist/")
# Patch in the correct extensions
HandlerClass.extensions_map['.js'] = 'text/javascript'
HandlerClass.extensions_map['.mjs'] = 'text/javascript'
# Run the server (like `python -m http.server` does) # Run the server (like `python -m http.server` does)
http.server.test(HandlerClass, port=8000) http.server.test(HandlerClass, port=8000)

View File

@@ -55,7 +55,7 @@
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */ "outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */ // "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */ // "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
@@ -104,5 +104,7 @@
/* Completeness */ /* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
} },
"include": ["./**/*.ts"],
"exclude": ["node_modules", "dist"]
} }