extends Node export var websocket_url = "ws://127.0.0.1:8080" var _client = WebSocketClient.new() var shoutbox_ui var chat_name func join_chat(username): chat_name = username _client.get_peer(1).put_packet(JSON.print({'type': "user_joined", 'name': username}).to_utf8()) func send_message(message): _client.get_peer(1).put_packet(JSON.print({'type': 'shout', 'name': chat_name, 'message': message}).to_utf8()) sync func receive_message(packet): shoutbox_ui.display_message(packet) func _ready(): add_to_group("shoutbox_pr") _client.connect("connection_closed", self, "_closed") _client.connect("connection_error", self, "_error") _client.connect("connection_established", self, "_connected") _client.connect("data_received", self, "_on_data_received") var err = _client.connect_to_url(websocket_url)#, ["lws-mirror-protocol"]) if err != OK: print_debug("Unable to connect") set_process(false) func _error(was_clean = false): print_debug("Error, clean: ", was_clean) set_process(false) func _closed(was_clean = false): print_debug("Closed, clean: ", was_clean) set_process(false) func _connected(protocol = ""): print_debug("Connected with protocol: ", protocol) _client.get_peer(1).put_packet(JSON.print({'type': 'test', 'message': "Client test packet"}).to_utf8()) shoutbox_ui.join_button.show() shoutbox_ui.username.show() func _on_data_received(): var json = JSON.parse(_client.get_peer(1).get_packet().get_string_from_utf8()) var packet = json.result if typeof(packet) != 18: print_debug("NOT A DICTIONARY") print_debug("Got data from server: ", packet) if packet['type'] == "shout" or packet['type'] == "server_message": receive_message(packet) func _process(_delta): _client.poll()