Merge branch 'main' of https://github.com/vaporvee/multiplayer-game-test
This commit is contained in:
@@ -11,5 +11,6 @@ config_version=5
|
|||||||
[application]
|
[application]
|
||||||
|
|
||||||
config/name="Multiplayer Game Test"
|
config/name="Multiplayer Game Test"
|
||||||
|
run/main_scene="res://scenes/ui/testinterface.tscn"
|
||||||
config/features=PackedStringArray("4.2", "Forward Plus")
|
config/features=PackedStringArray("4.2", "Forward Plus")
|
||||||
config/icon="res://icon.svg"
|
config/icon="res://icon.svg"
|
||||||
|
29
project/scenes/ui/testinterface.tscn
Normal file
29
project/scenes/ui/testinterface.tscn
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
[gd_scene load_steps=2 format=3 uid="uid://bsno04jv8uvxe"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" path="res://scripts/ui/testinterface.gd" id="1_g1tlq"]
|
||||||
|
|
||||||
|
[node name="TestInterface" type="Control"]
|
||||||
|
layout_mode = 3
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
script = ExtResource("1_g1tlq")
|
||||||
|
|
||||||
|
[node name="Button" type="Button" parent="."]
|
||||||
|
layout_mode = 1
|
||||||
|
anchors_preset = 8
|
||||||
|
anchor_left = 0.5
|
||||||
|
anchor_top = 0.5
|
||||||
|
anchor_right = 0.5
|
||||||
|
anchor_bottom = 0.5
|
||||||
|
offset_left = -108.5
|
||||||
|
offset_top = -56.0
|
||||||
|
offset_right = 108.5
|
||||||
|
offset_bottom = 56.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
text = "Test websocket"
|
||||||
|
|
||||||
|
[connection signal="pressed" from="Button" to="." method="_on_button_pressed"]
|
42
project/scripts/ui/testinterface.gd
Normal file
42
project/scripts/ui/testinterface.gd
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
extends Control
|
||||||
|
|
||||||
|
var websocket_url = "wss://acecore.lol:4477/ws"
|
||||||
|
var websocket_peer = WebSocketPeer.new()
|
||||||
|
|
||||||
|
var payload: Dictionary = {
|
||||||
|
"type": "broadcast",
|
||||||
|
"msg": "TESTBUTTON_PRESSED"
|
||||||
|
}
|
||||||
|
|
||||||
|
func _ready():
|
||||||
|
var error = websocket_peer.connect_to_url(websocket_url)
|
||||||
|
if error == OK:
|
||||||
|
print("Connected to WebSocket server")
|
||||||
|
send_message("Connected client")
|
||||||
|
else:
|
||||||
|
print("Failed to connect to WebSocket server")
|
||||||
|
|
||||||
|
func _process(_delta):
|
||||||
|
websocket_peer.poll()
|
||||||
|
var state = websocket_peer.get_ready_state()
|
||||||
|
if state == WebSocketPeer.STATE_OPEN:
|
||||||
|
while websocket_peer.get_available_packet_count():
|
||||||
|
print("Packet: ", websocket_peer.get_packet().get_string_from_utf8())
|
||||||
|
elif state == WebSocketPeer.STATE_CLOSING:
|
||||||
|
# Keep polling to achieve proper close.
|
||||||
|
pass
|
||||||
|
elif state == WebSocketPeer.STATE_CLOSED:
|
||||||
|
var code = websocket_peer.get_close_code()
|
||||||
|
var reason = websocket_peer.get_close_reason()
|
||||||
|
print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1])
|
||||||
|
set_process(false)
|
||||||
|
|
||||||
|
func send_message(message: String) -> void:
|
||||||
|
var state = websocket_peer.get_ready_state()
|
||||||
|
if state == WebSocketPeer.STATE_OPEN:
|
||||||
|
websocket_peer.send(str(payload).to_utf8_buffer())
|
||||||
|
else:
|
||||||
|
print("WebSocket connection is not open. Current state: ", state)
|
||||||
|
|
||||||
|
func _on_button_pressed():
|
||||||
|
send_message("Test button pressed")
|
Reference in New Issue
Block a user