extends SceneTree func _init() -> void: var socket = WebSocketPeer.new() # socket.supported_protocols = [] socket.connect_to_url("ws://127.0.0.1:13981") socket.set_no_delay(true) # Wait up to 1 second for the socket to connect. _wait_until_not(socket, WebSocketPeer.STATE_CONNECTING) if socket.get_ready_state() != WebSocketPeer.STATE_OPEN: push_error("could not connect"); quit(1); return var message := "" for arg in OS.get_cmdline_user_args(): if not message.is_empty(): message += " " # TODO: Escape (and handle) quote characters? # Surround empty string and those that contain spaces with quotes. if arg.is_empty() or arg.contains(" "): arg = "\"" + arg + "\"" message += arg socket.send_text(message) # Wait up to 1 second for the socket to close. # This ensures the message makes it through. _wait_until_not(socket, WebSocketPeer.STATE_OPEN) quit() ## Waits up to the specified amount for the socket to NOT be in the given state any longer. func _wait_until_not(socket: WebSocketPeer, state: int, msec := 1000) -> void: var start := Time.get_ticks_msec() while socket.get_ready_state() == state: if Time.get_ticks_msec() - start > msec: return socket.poll() # Update websocket state.