Skip to content

Instantly share code, notes, and snippets.

@nisovin
Last active April 29, 2024 17:36
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisovin/cf9dd74678641fb70902866c79692b17 to your computer and use it in GitHub Desktop.
Save nisovin/cf9dd74678641fb70902866c79692b17 to your computer and use it in GitHub Desktop.
Godot Web Server Configs

Godot Web Server Configs

For Godot 4 and the threads export in Godot 3, you need to set special headers on your web server in order to enable SharedArrayBuffer. There are some examples here for various web servers.

Python

The script below can be used to start a web server that sets the required headers. The easiest way to use this is to place this python script in your export folder and double click it to start the server. Then you can view your game at http://localhost:8000.

If you're curious about the Python web server that Godot itself uses for its "Run In Browser" feature, you can find that here:

https://github.com/godotengine/godot/blob/master/platform/web/serve.py

Apache

You can set headers in Apache in an .htaccess file placed in your project directory, assuming you have AllowOverride enabled. You can also place this configuration in your Apache config files. This could be placed in a <VirtualHost> directive or a <Directory> directive, or probably others. You will need to have mod_headers enabled.

Header set Cross-Origin-Opener-Policy "same-origin"
Header set Cross-Origin-Embedder-Policy "require-corp"

Nginx

You can add headers in your nginx config file, which will look something like this:

server {
  # ... other stuff here ...
  location / {
    # ... other stuff here ...
    add_header 'Cross-Origin-Opener-Policy' 'same-origin';
    add_header 'Cross-Origin-Embedder-Policy' 'require-corp';
  }
}

Itch.io

When editing your project, under "Embed options", check the box labeled "SharedArrayBuffer support".

Newgrounds

When editing your project, check the box labeled "Uses SharedArrayBuffer/Cross Origin Isolation".

#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys
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")
SimpleHTTPRequestHandler.end_headers(self)
if __name__ == '__main__':
test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
@ChronoDK
Copy link

ChronoDK commented Mar 9, 2023

Here's how I solved it in FastAPI:

class CorpHeadersMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next: Callable):
        response = await call_next(request)
        response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
        response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
        return response

app.add_middleware(CorpHeadersMiddleware)

@terminatorbs
Copy link

thanks, super useful content.
Quick question, do you happen to know how to add Websocket support too? I'm trying to make my multiplayer game work in the browser, but it won't connect and won't even print any errors. This is using nginx, i've tried adding headers "Upgrade" and "Connection" similar to how i added the cross-origin stuff but it's not working and i really can't find anything on google about it, it's all about proxying which isn't what i'm doing (is it?).

@DearFox
Copy link

DearFox commented Apr 11, 2023

thanks, super useful content. Quick question, do you happen to know how to add Websocket support too? I'm trying to make my multiplayer game work in the browser, but it won't connect and won't even print any errors. This is using nginx, i've tried adding headers "Upgrade" and "Connection" similar to how i added the cross-origin stuff but it's not working and i really can't find anything on google about it, it's all about proxying which isn't what i'm doing (is it?).

Are you trying to run the server in a browser?
Or are you trying to connect to a server that is running somewhere separately through the web version of the game? (and while other versions of the game work and connect).
Just as far as I remember - you can't create a server in the web version.
I don't really know much about this, I'm just saying what I remember from experience with Godot 3
If you have news on your problem - then write here, as I also want to make a multiplayer game on WebSocket in the future х)

And you mentioned proxying? I want to check if it is possible to connect via WebSocket to the server behind cloudflare (it seems like it should support this, since there are several ports that you can use for this)

@terminatorbs
Copy link

The game in the browser was supposed to be a client, connecting to a "normal" server. The idea behind it was that, during development, i could send a link to a friend and be like "playtest this with me for a minute".

I've experimented some more and researched some more but could not get it to work - iirc i also saw someone mention that HTML5 export in Godot 4 does not support websockets properly yet, so after a while i chalked it up to that and decided to try again later.

The documentation on websockets is also still outdated, so once that's up to date i might give it another shot. I'm actually still not sure if i need to allow websockets somehow in my nginx conf or if it really just isn't working properly in godot 4 web exports yet.

The mention about proxing is just because literally EVERY result on google in my search was about proxying things, but in my case i'm running it directly on the nginx server so these results didn't help me. or at least that's how i understand it.

@nisovin
Copy link
Author

nisovin commented Apr 12, 2023

@terminatorbs I'm not sure if these headers affect connections to a websocket server, but if they do, then you may need to add headers to the websocket server. Also, you will need to make sure your websocket server is running SSL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment