2014-11-13 03:53:12 +00:00
|
|
|
|
|
|
|
extends Panel
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
# Really simple UDP chat client, not intended as a comprehensive chat implementation.
|
2014-11-13 03:53:12 +00:00
|
|
|
# (UDP can lose packets and you won't normally find out, so don't do a chat this way)
|
|
|
|
# This is just a demo that shows how to use the UDP class.
|
|
|
|
|
2015-11-21 12:31:25 +00:00
|
|
|
# Member variables
|
2014-11-13 03:53:12 +00:00
|
|
|
var udp = PacketPeerUDP.new()
|
|
|
|
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
func _process(delta):
|
2014-11-13 03:53:12 +00:00
|
|
|
if (not udp.is_listening()):
|
|
|
|
return
|
2015-12-09 07:38:23 +00:00
|
|
|
|
|
|
|
while(udp.get_available_packet_count() > 0):
|
2014-11-13 03:53:12 +00:00
|
|
|
var packet = udp.get_var()
|
2015-12-09 07:38:23 +00:00
|
|
|
if (typeof(packet) == TYPE_STRING):
|
2014-11-13 03:53:12 +00:00
|
|
|
var host = udp.get_packet_ip()
|
|
|
|
var port = udp.get_packet_port()
|
2015-12-09 07:38:23 +00:00
|
|
|
get_node("chat/text").add_text("(" + host + ":" + str(port) + ":) " + packet)
|
2014-11-13 03:53:12 +00:00
|
|
|
get_node("chat/text").newline()
|
2015-12-09 07:38:23 +00:00
|
|
|
|
2014-11-13 03:53:12 +00:00
|
|
|
|
|
|
|
func _ready():
|
2015-12-09 07:38:23 +00:00
|
|
|
get_node("chat").add_style_override("panel", get_stylebox("bg", "Tree"))
|
2014-11-13 03:53:12 +00:00
|
|
|
set_process(true)
|
|
|
|
|
|
|
|
|
|
|
|
func send_message(text):
|
|
|
|
if (udp.is_listening()):
|
|
|
|
udp.put_var(text)
|
|
|
|
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
func _on_connect_toggled(pressed):
|
2014-11-13 03:53:12 +00:00
|
|
|
if (pressed):
|
2015-12-09 07:38:23 +00:00
|
|
|
var err = udp.listen(get_node("listen_port").get_val())
|
|
|
|
if (err != OK):
|
|
|
|
get_node("status").set_text("Error:\nCan't listen.")
|
2014-11-13 03:53:12 +00:00
|
|
|
get_node("connect").set_pressed(false)
|
|
|
|
else:
|
|
|
|
get_node("status").set_text("Connected.")
|
|
|
|
get_node("connect").set_text("Disconnect")
|
|
|
|
err = udp.set_send_address(get_node("remote_host").get_text(),get_node("remote_port").get_val())
|
2015-12-09 07:38:23 +00:00
|
|
|
if (err != OK):
|
|
|
|
get_node("status").set_text("Error:\nCan't resolve.")
|
2014-11-13 03:53:12 +00:00
|
|
|
get_node("connect").set_pressed(false)
|
|
|
|
else:
|
2015-12-09 07:38:23 +00:00
|
|
|
send_message("* " + get_node("user_name").get_text() + " entered chat.")
|
2014-11-13 03:53:12 +00:00
|
|
|
else:
|
|
|
|
udp.close()
|
|
|
|
get_node("status").set_text("Disconnected.")
|
|
|
|
get_node("connect").set_text("Connect")
|
|
|
|
|
|
|
|
|
2015-12-09 07:38:23 +00:00
|
|
|
func _on_entry_line_text_entered(text):
|
|
|
|
_on_entry_button_pressed()
|
2014-11-13 03:53:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _on_entry_button_pressed():
|
|
|
|
var msg = get_node("entry_line").get_text()
|
2015-12-09 07:38:23 +00:00
|
|
|
if (msg == ""):
|
2014-11-13 03:53:12 +00:00
|
|
|
return
|
2015-12-09 07:38:23 +00:00
|
|
|
send_message(get_node("user_name").get_text() + "> " + msg)
|
2014-11-13 03:53:12 +00:00
|
|
|
|
|
|
|
get_node("entry_line").set_text("")
|