part 3 - conversation
This commit is contained in:
@@ -6,13 +6,20 @@ onready var UI = $UI
|
||||
var chat_name
|
||||
|
||||
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):
|
||||
pass
|
||||
_client.get_peer(1).put_packet(JSON.print(
|
||||
{'type': 'chat_message', 'name': chat_name, 'message': message}
|
||||
).to_utf8()
|
||||
)
|
||||
|
||||
sync func receive_message(packet):
|
||||
pass
|
||||
func receive_message(packet):
|
||||
UI.display_message(packet)
|
||||
|
||||
func _ready():
|
||||
_client.connect("connection_closed", self, "_closed")
|
||||
@@ -27,22 +34,31 @@ func _ready():
|
||||
|
||||
func _error(was_clean = false):
|
||||
print_debug("Error. Clean break? ", was_clean)
|
||||
set_process(false)
|
||||
|
||||
func _closed(was_clean = false):
|
||||
print_debug("Closed. Clean break? ", 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': "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():
|
||||
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
|
||||
if typeof(packet) != 18:
|
||||
push_error("%s is not a dictionary" % [packet])
|
||||
get_tree().quit()
|
||||
print_debug("Got data from server: ", packet)
|
||||
if packet['type'] == "chat_message" or packet['type'] == "server_message":
|
||||
receive_message(packet)
|
||||
|
||||
func _process(_delta):
|
||||
_client.poll()
|
||||
|
||||
+3
-1
@@ -30,12 +30,14 @@ align = 1
|
||||
[node name="ChatLog" type="TextEdit" parent="UI/VBox"]
|
||||
margin_top = 18.0
|
||||
margin_right = 236.0
|
||||
margin_bottom = 530.0
|
||||
margin_bottom = 558.0
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 3
|
||||
show_line_numbers = true
|
||||
wrap_enabled = true
|
||||
|
||||
[node name="ChatInput" type="LineEdit" parent="UI/VBox"]
|
||||
visible = false
|
||||
margin_top = 534.0
|
||||
margin_right = 236.0
|
||||
margin_bottom = 558.0
|
||||
|
||||
@@ -5,14 +5,26 @@ onready var chat_input = $VBox/ChatInput
|
||||
onready var join_button = $VBox/HBox/JoinButton
|
||||
onready var username = $VBox/HBox/Name
|
||||
|
||||
func display_message(packet): # shout, alert, server_message, user_joined, user_left
|
||||
pass
|
||||
func display_message(packet):
|
||||
if packet['type'] == "chat_message":
|
||||
chat_log.text += packet['name'] + ": " + packet['message'] + "\n"
|
||||
elif packet.has("message"):
|
||||
chat_log.text += packet['message'] + "\n"
|
||||
|
||||
func join_chat():
|
||||
pass
|
||||
if !username.text:
|
||||
display_message(
|
||||
{'type': "alert",
|
||||
"message": "!!! Enter your username before joining chat."}
|
||||
)
|
||||
else:
|
||||
get_parent().join_chat(username.text)
|
||||
chat_input.show()
|
||||
join_button.hide()
|
||||
username.hide()
|
||||
|
||||
func _input(event):
|
||||
if event is InputEventKey:
|
||||
if event is InputEventKey and chat_input.text:
|
||||
if event.pressed and event.scancode == KEY_ENTER:
|
||||
get_parent().send_message(chat_input.text)
|
||||
chat_input.text = ""
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ custom_features=""
|
||||
export_filter="all_resources"
|
||||
include_filter=""
|
||||
exclude_filter=""
|
||||
export_path="../Exports/Chat Client.x86_64"
|
||||
export_path="../Exports/ChatClient.x86_64"
|
||||
script_export_mode=1
|
||||
script_encryption_key=""
|
||||
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ config_version=4
|
||||
|
||||
[application]
|
||||
|
||||
config/name="Chat Client"
|
||||
config/name="ChatClient"
|
||||
run/main_scene="res://Client.tscn"
|
||||
config/icon="res://icon.png"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user