OK, so what is happening is that wl_display_add_socket_auto(display) relies on $XDG_RUNTIME_DIR being set. That's a sensible default, but, as root, this isn't the case so no socket is created.
The client is looking in the right place, we just need a way for the script to specify the wayland socket to the server, and that needs to call wl_display_add_socket(<socket>, display) instead. (Much as we do for the legacy Mir socket.)
OK, so what is happening is that wl_display_ add_socket_ auto(display) relies on $XDG_RUNTIME_DIR being set. That's a sensible default, but, as root, this isn't the case so no socket is created.
The client is looking in the right place, we just need a way for the script to specify the wayland socket to the server, and that needs to call wl_display_ add_socket( <socket> , display) instead. (Much as we do for the legacy Mir socket.)