add websocket
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
extends Node
|
||||
|
||||
|
||||
# Declare member variables here. Examples:
|
||||
# var a = 2
|
||||
# var b = "text"
|
||||
|
||||
var url = "ws://127.0.0.1"
|
||||
var port = "1114"
|
||||
|
||||
# Our WebSocketClient instance
|
||||
var _client = WebSocketClient.new()
|
||||
|
||||
func _ready():
|
||||
# Connect base signals to get notified of connection open, close, and errors.
|
||||
_client.connect("connection_closed", self, "_closed")
|
||||
_client.connect("connection_error", self, "_closed")
|
||||
_client.connect("connection_established", self, "_connected")
|
||||
# This signal is emitted when not using the Multiplayer API every time
|
||||
# a full packet is received.
|
||||
# Alternatively, you could check get_peer(1).get_available_packets() in a loop.
|
||||
_client.connect("data_received", self, "_on_data")
|
||||
|
||||
# Initiate connection to the given URL.
|
||||
var err = _client.connect_to_url(url+":"+port)
|
||||
if err != OK:
|
||||
print("Unable to connect")
|
||||
set_process(false)
|
||||
|
||||
func send_message(msg):
|
||||
_client.get_peer(1).put_packet(msg.to_utf8())
|
||||
|
||||
func _closed(was_clean = false):
|
||||
# was_clean will tell you if the disconnection was correctly notified
|
||||
# by the remote peer before closing the socket.
|
||||
print("Closed, clean: ", was_clean)
|
||||
set_process(false)
|
||||
|
||||
func _connected(proto = ""):
|
||||
# This is called on connection, "proto" will be the selected WebSocket
|
||||
# sub-protocol (which is optional)
|
||||
print("Connected with protocol: ", proto)
|
||||
# You MUST always use get_peer(1).put_packet to send data to server,
|
||||
# and not put_packet directly when not using the MultiplayerAPI.
|
||||
_client.get_peer(1).put_packet("Test packet".to_utf8())
|
||||
|
||||
func _on_data():
|
||||
# Print the received packet, you MUST always use get_peer(1).get_packet
|
||||
# to receive data from server, and not get_packet directly when not
|
||||
# using the MultiplayerAPI.
|
||||
print("Got data from server: ", _client.get_peer(1).get_packet().get_string_from_utf8())
|
||||
|
||||
func _process(delta):
|
||||
# Call this in _process or _physics_process. Data transfer, and signals
|
||||
# emission will only happen when calling this function.
|
||||
_client.poll()
|
||||
+1
-1
@@ -6,7 +6,7 @@ extends Node
|
||||
# var b = "text"
|
||||
|
||||
class Tile:
|
||||
enum Suit {Character, Bamboo, Dot, Bonus, Wind, Dragon, Flower, Season}
|
||||
enum Suit {Character, Bamboo, Dot, Wind, Dragon, Flower, Season}
|
||||
const HEIGHT = 142
|
||||
const WIDTH = 90
|
||||
const SELECT_HEIGHT = 30
|
||||
|
||||
@@ -59,3 +59,8 @@ func _on_Button7_pressed():
|
||||
func _on_Button8_pressed():
|
||||
$LeftDropArea.add_tile(randi()%3, randi()%9)
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
func _on_Button9_pressed():
|
||||
ConnManager.send_message($HBoxContainer/TextEdit.text)
|
||||
|
||||
|
||||
+13
@@ -152,6 +152,18 @@ margin_right = 856.0
|
||||
margin_bottom = 40.0
|
||||
text = "AddLeftDrop"
|
||||
|
||||
[node name="Button9" type="Button" parent="HBoxContainer"]
|
||||
margin_left = 866.0
|
||||
margin_right = 970.0
|
||||
margin_bottom = 40.0
|
||||
text = "Send to Server"
|
||||
|
||||
[node name="TextEdit" type="TextEdit" parent="HBoxContainer"]
|
||||
margin_left = 980.0
|
||||
margin_right = 1080.0
|
||||
margin_bottom = 40.0
|
||||
rect_min_size = Vector2( 50, 0 )
|
||||
|
||||
[node name="BottomDropArea" type="Node2D" parent="."]
|
||||
position = Vector2( 621.315, 779.207 )
|
||||
script = ExtResource( 13 )
|
||||
@@ -175,3 +187,4 @@ script = ExtResource( 15 )
|
||||
[connection signal="pressed" from="HBoxContainer/Button6" to="." method="_on_Button6_pressed"]
|
||||
[connection signal="pressed" from="HBoxContainer/Button7" to="." method="_on_Button7_pressed"]
|
||||
[connection signal="pressed" from="HBoxContainer/Button8" to="." method="_on_Button8_pressed"]
|
||||
[connection signal="pressed" from="HBoxContainer/Button9" to="." method="_on_Button9_pressed"]
|
||||
|
||||
@@ -21,6 +21,7 @@ config/icon="res://icon.png"
|
||||
[autoload]
|
||||
|
||||
Constants="*res://Constants.gd"
|
||||
ConnManager="*res://ConnManager.gd"
|
||||
|
||||
[display]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user