adding on client input ip/port to connect to server

This commit is contained in:
AleaJactaEst 2023-10-23 23:09:33 +02:00
parent 70f59e12af
commit 91e04a00a7
3 changed files with 43 additions and 83 deletions

View file

@ -3,6 +3,9 @@ extends Window
var focus_ok:bool = false var focus_ok:bool = false
var last_event = null var last_event = null
@export var listen_ip:String = ""
@export var listen_port:int = 33333
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
pass # Replace with function body. pass # Replace with function body.
@ -17,14 +20,25 @@ func _on_button_pressed():
if $VBoxContainer/TextEdit.get_text() != "": if $VBoxContainer/TextEdit.get_text() != "":
self.hide() self.hide()
#get_parent().get_node("CharacterBody3D").set_enable_event(true) #get_parent().get_node("CharacterBody3D").set_enable_event(true)
get_parent().connect_enet($VBoxContainer/TextEdit.get_text()) get_parent().connect_enet($VBoxContainer/TextEdit.get_text(), listen_ip, listen_port)
func check_testedit():
if len($VBoxContainer/TextEdit.get_text()) == 0:
$VBoxContainer/Button.set_disabled(true)
return
if len($VBoxContainer/Address.get_text()) == 0:
$VBoxContainer/Button.set_disabled(true)
return
listen_ip = $VBoxContainer/Address.get_text()
listen_port = int($VBoxContainer/Port.get_text())
if listen_port <= 0 || listen_port >= 65536 :
$VBoxContainer/Button.set_disabled(true)
return
$VBoxContainer/Button.set_disabled(false)
func _on_text_edit_text_changed(value:String): func _on_text_edit_text_changed(value:String):
if len(value) > 0: check_testedit()
$VBoxContainer/Button.disabled = false
else:
$VBoxContainer/Button.disabled = true
func _on_focus_entered(): func _on_focus_entered():

View file

@ -14,50 +14,26 @@ var account_confirmed:bool = false
const PLAYER = preload("res://scenes/player.tscn") const PLAYER = preload("res://scenes/player.tscn")
const PORT = 33333
const ADDR = "127.0.0.1"
var dtls := PacketPeerDTLS.new() var dtls := PacketPeerDTLS.new()
var udp := PacketPeerUDP.new() var udp := PacketPeerUDP.new()
func test_cert(filename): func create_server_enet(address, port):
print("------", filename) print("------ create_server_enet")
enet = ENetConnection.new()
print(enet)
#var cert: X509Certificate = X509Certificate.new()
#errorEnet = cert.load(filename)
#print("load cert:", errorEnet)
#print(cert)
#var client_tls_options = TLSOptions.client_unsafe(cert)
#print(client_tls_options)
errorEnet = enet.create_host(10) errorEnet = enet.create_host(10)
if errorEnet != OK: if errorEnet != OK:
print("ERROR ENET.create_host: ", errorEnet) print("ERROR ENET.create_host: ", errorEnet)
return return
#enet.connect_to_host(ADDR, PORT) print("Connect to : " + address + " : " + str(port))
#errorEnet = enet.dtls_client_setup(ADDR, client_tls_options) dataEnet = enet.connect_to_host(address, port, 10)
#print("dtls_client_setup:", errorEnet)
#if errorEnet != OK:
# print("ERROR ENET: ", errorEnet)
# return
dataEnet = enet.connect_to_host(ADDR, PORT, 10)
print(dataEnet) print(dataEnet)
if not dataEnet.is_active(): if not dataEnet.is_active():
print("ERROR enet.connect_to_host: ", dataEnet.is_active()) print("ERROR enet.connect_to_host: ", dataEnet.is_active())
return return
func create_server_enet():
print("------ create_server_enet")
test_cert("res://cert.crt")
# Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
#test_cert("res://cert.crt") enet = ENetConnection.new()
create_server_enet()
pass # Replace with function body.
func decode_msg(data:PackedByteArray): func decode_msg(data:PackedByteArray):
@ -197,6 +173,8 @@ func get_event_received():
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta): func _process(_delta):
if not dataEnet:
return
#print("state:", dataEnet.get_state()) #print("state:", dataEnet.get_state())
get_player_position() get_player_position()
if dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTED: if dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTED:
@ -246,54 +224,8 @@ func _process_ter(_delta):
return return
func _process_bis(_delta): func connect_enet(_name:String, listen_ip:String, listen_port:int):
if not dataEnet.is_active():
create_server_enet()
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTING:
var res = enet.service(1)
print(res)
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTED:
var res = enet.service(1)
print(res)
var data:PackedByteArray = PackedByteArray()
data.append(len(player_name))
data.append(123)
errorEnet = dataEnet.send(1, data, 1)
if errorEnet != OK:
print("ERROR ENET: ", errorEnet)
return
#print("<> get_state: ", dataEnet.get_state())
else:
print("get_state: ", dataEnet.get_state())
return
if not connected:
if player_name != "":
create_server_enet()
return
var res = enet.service(1)
print(res)
return
if not connected:
if player_name != "":
create_server_enet()
else:
var data:PackedByteArray = PackedByteArray()
data.append(len(player_name))
data.append(123)
#print(len(data))
print("active:", dataEnet.is_active())
print("state:", dataEnet.get_state())
print("address:", dataEnet.get_remote_address())
print("port:", dataEnet.get_remote_port())
errorEnet = dataEnet.send(1, data, 1)
if errorEnet != OK:
print("ERROR ENET: ", errorEnet)
return
#enet.socket_send(ADDR, PORT, data)
func connect_enet(_name:String):
player_name = _name player_name = _name
create_server_enet(listen_ip, listen_port)
print("My name:" + player_name) print("My name:" + player_name)

View file

@ -56,7 +56,7 @@ environment = SubResource("Environment_12j7q")
[node name="Window" type="Window" parent="."] [node name="Window" type="Window" parent="."]
title = "Connexion" title = "Connexion"
initial_position = 1 initial_position = 1
size = Vector2i(300, 100) size = Vector2i(300, 200)
script = ExtResource("3_uwnj8") script = ExtResource("3_uwnj8")
[node name="VBoxContainer" type="VBoxContainer" parent="Window"] [node name="VBoxContainer" type="VBoxContainer" parent="Window"]
@ -71,6 +71,18 @@ layout_mode = 2
text = "Login" text = "Login"
horizontal_alignment = 1 horizontal_alignment = 1
[node name="Address" type="LineEdit" parent="Window/VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
text = "127.0.0.1"
placeholder_text = "Address"
[node name="Port" type="LineEdit" parent="Window/VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
text = "33333"
placeholder_text = "Address"
[node name="TextEdit" type="LineEdit" parent="Window/VBoxContainer"] [node name="TextEdit" type="LineEdit" parent="Window/VBoxContainer"]
layout_mode = 2 layout_mode = 2
size_flags_vertical = 3 size_flags_vertical = 3
@ -85,5 +97,7 @@ text = "OK"
[connection signal="focus_exited" from="Window" to="Window" method="_on_focus_exited"] [connection signal="focus_exited" from="Window" to="Window" method="_on_focus_exited"]
[connection signal="mouse_entered" from="Window" to="Window" method="_on_mouse_entered"] [connection signal="mouse_entered" from="Window" to="Window" method="_on_mouse_entered"]
[connection signal="mouse_exited" from="Window" to="Window" method="_on_mouse_exited"] [connection signal="mouse_exited" from="Window" to="Window" method="_on_mouse_exited"]
[connection signal="text_changed" from="Window/VBoxContainer/Address" to="Window" method="_on_text_edit_text_changed"]
[connection signal="text_changed" from="Window/VBoxContainer/Port" to="Window" method="_on_text_edit_text_changed"]
[connection signal="text_changed" from="Window/VBoxContainer/TextEdit" to="Window" method="_on_text_edit_text_changed"] [connection signal="text_changed" from="Window/VBoxContainer/TextEdit" to="Window" method="_on_text_edit_text_changed"]
[connection signal="pressed" from="Window/VBoxContainer/Button" to="Window" method="_on_button_pressed"] [connection signal="pressed" from="Window/VBoxContainer/Button" to="Window" method="_on_button_pressed"]