Skip to content

Commit e9f02ff

Browse files
committed
Add Websocket chat demo
1 parent 949a942 commit e9f02ff

File tree

11 files changed

+667
-0
lines changed

11 files changed

+667
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
extends Node
2+
3+
onready var _log_dest = get_parent().get_node("Panel/VBoxContainer/RichTextLabel")
4+
5+
var _client = WebSocketClient.new()
6+
var _write_mode = WebSocketPeer.WRITE_MODE_BINARY
7+
var _use_multiplayer = true
8+
var last_connected_client = 0
9+
10+
func _init():
11+
_client.connect("connection_established", self, "_client_connected")
12+
_client.connect("connection_error", self, "_client_disconnected")
13+
_client.connect("connection_closed", self, "_client_disconnected")
14+
_client.connect("server_close_request", self, "_client_close_request")
15+
_client.connect("data_received", self, "_client_received")
16+
17+
_client.connect("peer_packet", self, "_client_received")
18+
_client.connect("peer_connected", self, "_peer_connected")
19+
_client.connect("connection_succeeded", self, "_client_connected", ["multiplayer_protocol"])
20+
_client.connect("connection_failed", self, "_client_disconnected")
21+
22+
func _client_close_request(code, reason):
23+
Utils._log(_log_dest, "Close code: %d, reason: %s" % [code, reason])
24+
25+
func _peer_connected(id):
26+
Utils._log(_log_dest, "%s: Client just connected" % id)
27+
last_connected_client = id
28+
29+
func _exit_tree():
30+
_client.disconnect_from_host(1001, "Bye bye!")
31+
32+
func _process(delta):
33+
if _client.get_connection_status() == WebSocketClient.CONNECTION_DISCONNECTED:
34+
return
35+
36+
_client.poll()
37+
38+
func _client_connected(protocol):
39+
Utils._log(_log_dest, "Client just connected with protocol: %s" % protocol)
40+
_client.get_peer(1).set_write_mode(_write_mode)
41+
42+
func _client_disconnected(clean=true):
43+
Utils._log(_log_dest, "Client just disconnected. Was clean: %s" % clean)
44+
45+
func _client_received(p_id = 1):
46+
if _use_multiplayer:
47+
var peer_id = _client.get_packet_peer()
48+
var packet = _client.get_packet()
49+
Utils._log(_log_dest, "MPAPI: From %s Data: %s" % [str(peer_id), Utils.decode_data(packet, false)])
50+
else:
51+
var packet = _client.get_peer(1).get_packet()
52+
var is_string = _client.get_peer(1).was_string_packet()
53+
Utils._log(_log_dest, "Received data. BINARY: %s: %s" % [not is_string, Utils.decode_data(packet, is_string)])
54+
55+
func connect_to_url(host, protocols, multiplayer):
56+
_use_multiplayer = multiplayer
57+
if _use_multiplayer:
58+
_write_mode = WebSocketPeer.WRITE_MODE_BINARY
59+
return _client.connect_to_url(host, protocols, multiplayer)
60+
61+
func disconnect_from_host():
62+
_client.disconnect_from_host(1000, "Bye bye!")
63+
64+
func send_data(data, dest):
65+
_client.get_peer(1).set_write_mode(_write_mode)
66+
if _use_multiplayer:
67+
_client.set_target_peer(dest)
68+
_client.put_packet(Utils.encode_data(data, _write_mode))
69+
else:
70+
_client.get_peer(1).put_packet(Utils.encode_data(data, _write_mode))
71+
72+
func set_write_mode(mode):
73+
_write_mode = mode
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
[gd_scene load_steps=3 format=2]
2+
3+
[ext_resource path="res://client/client_ui.gd" type="Script" id=1]
4+
[ext_resource path="res://client/client.gd" type="Script" id=2]
5+
6+
[node name="Client" type="Control"]
7+
anchor_right = 1.0
8+
anchor_bottom = 1.0
9+
script = ExtResource( 1 )
10+
__meta__ = {
11+
"_edit_use_anchors_": false
12+
}
13+
14+
[node name="Panel" type="Panel" parent="."]
15+
anchor_right = 1.0
16+
anchor_bottom = 1.0
17+
__meta__ = {
18+
19+
}
20+
21+
[node name="VBoxContainer" type="VBoxContainer" parent="Panel"]
22+
anchor_right = 1.0
23+
anchor_bottom = 1.0
24+
__meta__ = {
25+
26+
}
27+
28+
[node name="Connect" type="HBoxContainer" parent="Panel/VBoxContainer"]
29+
margin_right = 1024.0
30+
margin_bottom = 24.0
31+
__meta__ = {
32+
33+
}
34+
35+
[node name="Host" type="LineEdit" parent="Panel/VBoxContainer/Connect"]
36+
margin_right = 956.0
37+
margin_bottom = 24.0
38+
size_flags_horizontal = 3
39+
text = "ws://localhost:8000/test/"
40+
placeholder_text = "ws://my.server/path/"
41+
__meta__ = {
42+
43+
}
44+
45+
[node name="Connect" type="Button" parent="Panel/VBoxContainer/Connect"]
46+
margin_left = 960.0
47+
margin_right = 1024.0
48+
margin_bottom = 24.0
49+
toggle_mode = true
50+
text = "Connect"
51+
__meta__ = {
52+
53+
}
54+
55+
[node name="Settings" type="HBoxContainer" parent="Panel/VBoxContainer"]
56+
margin_top = 28.0
57+
margin_right = 1024.0
58+
margin_bottom = 52.0
59+
__meta__ = {
60+
61+
}
62+
63+
[node name="Mode" type="OptionButton" parent="Panel/VBoxContainer/Settings"]
64+
margin_right = 41.0
65+
margin_bottom = 24.0
66+
__meta__ = {
67+
68+
}
69+
70+
[node name="Multiplayer" type="CheckBox" parent="Panel/VBoxContainer/Settings"]
71+
margin_left = 45.0
72+
margin_right = 171.0
73+
margin_bottom = 24.0
74+
pressed = true
75+
text = "Multiplayer API"
76+
__meta__ = {
77+
78+
}
79+
80+
[node name="Destination" type="OptionButton" parent="Panel/VBoxContainer/Settings"]
81+
margin_left = 175.0
82+
margin_right = 216.0
83+
margin_bottom = 24.0
84+
__meta__ = {
85+
86+
}
87+
88+
[node name="Send" type="HBoxContainer" parent="Panel/VBoxContainer"]
89+
margin_top = 56.0
90+
margin_right = 1024.0
91+
margin_bottom = 80.0
92+
__meta__ = {
93+
94+
}
95+
96+
[node name="LineEdit" type="LineEdit" parent="Panel/VBoxContainer/Send"]
97+
margin_right = 977.0
98+
margin_bottom = 24.0
99+
size_flags_horizontal = 3
100+
placeholder_text = "Enter some text to send..."
101+
__meta__ = {
102+
103+
}
104+
105+
[node name="Send" type="Button" parent="Panel/VBoxContainer/Send"]
106+
margin_left = 981.0
107+
margin_right = 1024.0
108+
margin_bottom = 24.0
109+
text = "Send"
110+
__meta__ = {
111+
112+
}
113+
114+
[node name="RichTextLabel" type="RichTextLabel" parent="Panel/VBoxContainer"]
115+
margin_top = 84.0
116+
margin_right = 1024.0
117+
margin_bottom = 600.0
118+
size_flags_vertical = 3
119+
__meta__ = {
120+
121+
}
122+
123+
[node name="Client" type="Node" parent="."]
124+
script = ExtResource( 2 )
125+
__meta__ = {
126+
127+
}
128+
[connection signal="toggled" from="Panel/VBoxContainer/Connect/Connect" to="." method="_on_Connect_toggled"]
129+
[connection signal="item_selected" from="Panel/VBoxContainer/Settings/Mode" to="." method="_on_Mode_item_selected"]
130+
[connection signal="pressed" from="Panel/VBoxContainer/Send/Send" to="." method="_on_Send_pressed"]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
extends Control
2+
3+
onready var _client = get_node("Client")
4+
onready var _log_dest = get_node("Panel/VBoxContainer/RichTextLabel")
5+
onready var _line_edit = get_node("Panel/VBoxContainer/Send/LineEdit")
6+
onready var _host = get_node("Panel/VBoxContainer/Connect/Host")
7+
onready var _multiplayer = get_node("Panel/VBoxContainer/Settings/Multiplayer")
8+
onready var _write_mode = get_node("Panel/VBoxContainer/Settings/Mode")
9+
onready var _destination = get_node("Panel/VBoxContainer/Settings/Destination")
10+
11+
func _ready():
12+
_write_mode.clear()
13+
_write_mode.add_item("BINARY")
14+
_write_mode.set_item_metadata(0, WebSocketPeer.WRITE_MODE_BINARY)
15+
_write_mode.add_item("TEXT")
16+
_write_mode.set_item_metadata(1, WebSocketPeer.WRITE_MODE_TEXT)
17+
18+
_destination.add_item("Broadcast")
19+
_destination.set_item_metadata(0, 0)
20+
_destination.add_item("Last connected")
21+
_destination.set_item_metadata(1, 1)
22+
_destination.add_item("All But last connected")
23+
_destination.set_item_metadata(2, -1)
24+
_destination.select(0)
25+
26+
func _on_Mode_item_selected( ID ):
27+
_client.set_write_mode(_write_mode.get_selected_metadata())
28+
29+
func _on_Send_pressed():
30+
if _line_edit.text == "":
31+
return
32+
33+
var dest = _destination.get_selected_metadata()
34+
if dest > 0:
35+
dest = _client.last_connected_client
36+
elif dest < 0:
37+
dest = -_client.last_connected_client
38+
39+
Utils._log(_log_dest, "Sending data %s to %s" % [_line_edit.text, dest])
40+
_client.send_data(_line_edit.text, dest)
41+
_line_edit.text = ""
42+
43+
func _on_Connect_toggled( pressed ):
44+
if pressed:
45+
var multiplayer = _multiplayer.pressed
46+
if multiplayer:
47+
_write_mode.disabled = true
48+
else:
49+
_destination.disabled = true
50+
_multiplayer.disabled = true
51+
if _host.text != "":
52+
Utils._log(_log_dest, "Connecting to host: %s" % [_host.text])
53+
var supported_protocols = PoolStringArray(["my-protocol2", "my-protocol", "binary"])
54+
_client.connect_to_url(_host.text, supported_protocols, multiplayer)
55+
else:
56+
_destination.disabled = false
57+
_write_mode.disabled = false
58+
_multiplayer.disabled = false
59+
_client.disconnect_from_host()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[gd_scene load_steps=3 format=2]
2+
3+
[ext_resource path="res://server/server.tscn" type="PackedScene" id=1]
4+
[ext_resource path="res://client/client.tscn" type="PackedScene" id=2]
5+
6+
[node name="Combo" type="Control"]
7+
anchor_right = 1.0
8+
anchor_bottom = 1.0
9+
mouse_filter = 1
10+
__meta__ = {
11+
12+
}
13+
14+
[node name="Box" type="HBoxContainer" parent="."]
15+
anchor_right = 1.0
16+
anchor_bottom = 1.0
17+
custom_constants/separation = 20
18+
__meta__ = {
19+
20+
}
21+
22+
[node name="ServerControl" parent="Box" instance=ExtResource( 1 )]
23+
anchor_right = 0.0
24+
anchor_bottom = 0.0
25+
margin_right = 502.0
26+
margin_bottom = 600.0
27+
size_flags_horizontal = 3
28+
29+
[node name="VBoxContainer" type="VBoxContainer" parent="Box"]
30+
margin_left = 522.0
31+
margin_right = 1024.0
32+
margin_bottom = 600.0
33+
size_flags_horizontal = 3
34+
__meta__ = {
35+
36+
}
37+
38+
[node name="Client" parent="Box/VBoxContainer" instance=ExtResource( 2 )]
39+
anchor_right = 0.0
40+
anchor_bottom = 0.0
41+
margin_right = 502.0
42+
margin_bottom = 197.0
43+
size_flags_horizontal = 3
44+
size_flags_vertical = 3
45+
46+
[node name="Client2" parent="Box/VBoxContainer" instance=ExtResource( 2 )]
47+
anchor_right = 0.0
48+
anchor_bottom = 0.0
49+
margin_top = 201.0
50+
margin_right = 502.0
51+
margin_bottom = 398.0
52+
size_flags_horizontal = 3
53+
size_flags_vertical = 3
54+
55+
[node name="Client3" parent="Box/VBoxContainer" instance=ExtResource( 2 )]
56+
anchor_right = 0.0
57+
anchor_bottom = 0.0
58+
margin_top = 402.0
59+
margin_right = 502.0
60+
margin_bottom = 600.0
61+
size_flags_horizontal = 3
62+
size_flags_vertical = 3

networking/websocket_chat/icon.png

3.42 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://icon.png"
13+
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ ]
12+
_global_script_class_icons={
13+
14+
}
15+
16+
[application]
17+
18+
config/name="Websocket Chat Demo"
19+
run/main_scene="res://combo/combo.tscn"
20+
config/icon="res://icon.png"
21+
22+
[autoload]
23+
24+
Utils="*res://utils.gd"
25+
26+
[gdnative]
27+
28+
singletons=[ ]

0 commit comments

Comments
 (0)