|
|
@ -6,13 +6,20 @@ onready var UI = $UI
|
|
|
|
var chat_name
|
|
|
|
var chat_name
|
|
|
|
|
|
|
|
|
|
|
|
func join_chat(username):
|
|
|
|
func join_chat(username):
|
|
|
|
pass
|
|
|
|
chat_name = username
|
|
|
|
|
|
|
|
_client.get_peer(1).put_packet(JSON.print(
|
|
|
|
|
|
|
|
{'type': "user_joined", 'name': username}
|
|
|
|
|
|
|
|
).to_utf8()
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func send_message(message):
|
|
|
|
func send_message(message):
|
|
|
|
pass
|
|
|
|
_client.get_peer(1).put_packet(JSON.print(
|
|
|
|
|
|
|
|
{'type': 'chat_message', 'name': chat_name, 'message': message}
|
|
|
|
|
|
|
|
).to_utf8()
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
sync func receive_message(packet):
|
|
|
|
func receive_message(packet):
|
|
|
|
pass
|
|
|
|
UI.display_message(packet)
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
func _ready():
|
|
|
|
_client.connect("connection_closed", self, "_closed")
|
|
|
|
_client.connect("connection_closed", self, "_closed")
|
|
|
@ -27,22 +34,31 @@ func _ready():
|
|
|
|
|
|
|
|
|
|
|
|
func _error(was_clean = false):
|
|
|
|
func _error(was_clean = false):
|
|
|
|
print_debug("Error. Clean break? ", was_clean)
|
|
|
|
print_debug("Error. Clean break? ", was_clean)
|
|
|
|
|
|
|
|
set_process(false)
|
|
|
|
|
|
|
|
|
|
|
|
func _closed(was_clean = false):
|
|
|
|
func _closed(was_clean = false):
|
|
|
|
print_debug("Closed. Clean break? ", was_clean)
|
|
|
|
print_debug("Closed. Clean break? ", was_clean)
|
|
|
|
|
|
|
|
set_process(false)
|
|
|
|
|
|
|
|
|
|
|
|
func _connected(protocol = ""):
|
|
|
|
func _connected(protocol = ""):
|
|
|
|
print_debug("Connected with protocol: ", protocol)
|
|
|
|
print_debug("Connected with protocol: ", protocol)
|
|
|
|
_client.get_peer(1).put_packet(JSON.print(
|
|
|
|
_client.get_peer(1).put_packet(JSON.print(
|
|
|
|
{'type': 'test', 'message': "test packet from client"}).to_utf8())
|
|
|
|
{'type': 'test', 'message': "test packet from client"}
|
|
|
|
|
|
|
|
).to_utf8())
|
|
|
|
|
|
|
|
UI.join_button.show()
|
|
|
|
|
|
|
|
UI.username.show()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_data_received():
|
|
|
|
func _on_data_received():
|
|
|
|
var json = JSON.parse(_client.get_peer(1).get_packet().get_string_from_utf8())
|
|
|
|
var json = JSON.parse(
|
|
|
|
|
|
|
|
_client.get_peer(1).get_packet().get_string_from_utf8()
|
|
|
|
|
|
|
|
)
|
|
|
|
var packet = json.result
|
|
|
|
var packet = json.result
|
|
|
|
if typeof(packet) != 18:
|
|
|
|
if typeof(packet) != 18:
|
|
|
|
push_error("%s is not a dictionary" % [packet])
|
|
|
|
push_error("%s is not a dictionary" % [packet])
|
|
|
|
get_tree().quit()
|
|
|
|
get_tree().quit()
|
|
|
|
print_debug("Got data from server: ", packet)
|
|
|
|
print_debug("Got data from server: ", packet)
|
|
|
|
|
|
|
|
if packet['type'] == "chat_message" or packet['type'] == "server_message":
|
|
|
|
|
|
|
|
receive_message(packet)
|
|
|
|
|
|
|
|
|
|
|
|
func _process(_delta):
|
|
|
|
func _process(_delta):
|
|
|
|
_client.poll()
|
|
|
|
_client.poll()
|
|
|
|