extends Control var server := "localhost" var port := "3784" var ws := WebSocketPeer.new() var mb_scene := preload("res://scenes/MultiMorseBanner.tscn") var available_freqs = [] func _ready() -> void: print("ws://%s:%s" % [server, port]) ws.connect_to_url("ws://%s:%s" % [server, port]) func refresh_list() -> void: pass func _process(_delta: float) -> void: ws.poll() var state := ws.get_ready_state() # print("moin ", state) while state == WebSocketPeer.STATE_OPEN and ws.get_available_packet_count(): _handle_packet() func _handle_packet() -> void: var data := ws.get_packet().get_string_from_utf8() print(data) var parsed: Dictionary = JSON.parse_string(data) if typeof(parsed) != TYPE_DICTIONARY or not parsed.has("type"): return print("parsed ", parsed) match parsed["type"]: "join": _join_freq(parsed["freq"]) "freq-list": %FreqList.clear() for freq in parsed["freqs"]: var text = "%s MHz (%d present)" % [freq["freq"], freq["players"]] print("Adding ", text) var idx: int = %FreqList.add_item(text) %FreqList.set_item_metadata(idx, freq) func _join_freq(freq: String): %FreqLabel.text = "%s MHz" % freq %ConnectView.hide() %MorseView.show() func _on_refresh_button_pressed() -> void: var refresh_cmd := {"cmd": "list", "type": "cw-generator"} var data := JSON.stringify(refresh_cmd) + "\n" ws.send_text(data) func _on_create_button_pressed() -> void: var freq: String = "%.3f" % float(%FrequencyCreator.text) var refresh_cmd := {"cmd": "create", "type": "cw-generator", "freq": freq} var data := JSON.stringify(refresh_cmd) + "\n" ws.send_text(data) func _on_freq_list_join(index: int, at_position: Vector2, mouse_button_index: int) -> void: var meta = %FreqList.get_item_metadata(index) var freq: String = meta["freq"] print("Yop ", index, " metadata ", freq) var join_cmd := {"cmd": "join", "freq": freq, "type": "cw-generator"} ws.send_text(JSON.stringify(join_cmd) + "\n")