I want to make an HTML game that I can test across multiple devices. The easiest way to do it is to run it on a server binded to 0.0.0.0
Godot allows me run a game using “Run in Browser” but this is binded to “localhost” only. So any requests coming in from another device in the same network is rejected.
The workaround I can think is to do an export html to file system and then start a local http server using some other software. This is a pain.
You can connect to a “Run in Browser”/“Start HTTP Server” debugging session from another device, I’ve done this to test HTML mobile games and HTML VR games. Can you explain how exactly you are trying to do so?
It is quite easy to set up Itch.io and Godot in a way which publishes your game with one button click from inside Godot to Itch. But one would not publish to Itch after changing one line of code, but only for major releases.
If you run something on a server you would probably have to configure Apache or nginx in the long run. Itch lets you host games without having to know anything about server configuration of networking.
Godot allows you to export your games for many platforms, the web is only one option (next to iOS, Android, Linux, Mac, Windows). But when doing such exports you would actually write your game in Godot and never have to touch on HTML or JavaScript. You would write your game in Godot and the export option would handle the rest (see documentation on export options).
If your game does not require it or if you don’t explicitly want to learn these technologies, I can highly recommend exporting directly from Godot to Itch. It really is quite easy once setup correctly.
localhost means “the computer this web browser is running on” You need to find your local ip address, if on windows the command line utility ipconfig will show it, on linux ip addr will do the same. You must enter your server machine’s ip address into your other device, for example http://192.168.1.123:8060/tmp_js_export.html
I do this all the time in game jams. Especially the alst day. I fix a bug, upload it, test it. The key to this is to use butler. You can download it for free on itch. The benefit is it creates a patch and only uploads that patch. So if you only change a line of code, 1% or less of your code is actually getting pushed up. You lso can declare in the upload it is for web, and it does all the fiddly bits for you.
Here’s a python script you can use to run a server on your box.
Web Server Python Script
#!/usr/bin/env python3
import argparse
import contextlib
import os
import socket
import subprocess
import sys
from http.server import HTTPServer, SimpleHTTPRequestHandler, test # type: ignore
from pathlib import Path
# See cpython GH-17851 and GH-17864.
class DualStackServer(HTTPServer):
def server_bind(self):
# Suppress exception when protocol is IPv4.
with contextlib.suppress(Exception):
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
return super().server_bind()
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
self.send_header("Access-Control-Allow-Origin", "*")
super().end_headers()
def shell_open(url):
if sys.platform == "win32":
os.startfile(url)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, url])
def serve(root, port, run_browser):
os.chdir(root)
if run_browser:
# Open the served page in the user's default browser.
print("Opening the served URL in the default browser (use `--no-browser` or `-n` to disable this).")
shell_open(f"http://127.0.0.1:{port}")
test(CORSRequestHandler, DualStackServer, port=port)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", help="port to listen on", default=8060, type=int)
parser.add_argument(
"-r", "--root", help="path to serve as root (relative to `platform/web/`)", default="../../bin", type=Path
)
browser_parser = parser.add_mutually_exclusive_group(required=False)
browser_parser.add_argument(
"-n", "--no-browser", help="don't open default web browser automatically", dest="browser", action="store_false"
)
parser.set_defaults(browser=True)
args = parser.parse_args()
# Change to the directory where the script is located,
# so that the script can be run from any location.
os.chdir(Path(__file__).resolve().parent)
serve(args.root, args.port, args.browser)
On Windows you can use this batch script to run it:
REM Place this file and serve.py into the folder with an exported Godot HTML file to test it.
REM Double-click this to launch the test server.
start powershell -command "python serve.py --root ."
On linux just make a shell script to run it.
python3 serve.py --root .
You may have to play with the shell command, it’s been a few years since I’ve used Linux and I just guessed at it..
P.S. Putting your server on 0.0.0.0 is a really bad idea. It’s a special IP and will take a connection from anything your computer is connected to. It’s really useful backdoor for hackers.
This is only half true.
If you’re sitting behind a firewall on a local network, and you have no port forwarding or DMZ set up, then the outside world has no access to your PC still, so you’re fine. And I imagine that’s likely the case here, this is (hopefully) not for production use.