[gd_scene load_steps=3 format=2] [sub_resource type="GDScript" id=1] script/source = "extends Control signal ws_out(msg) signal ws_login_in(msg) signal ws_candidate_in(mid, index, sdp) signal ws_sesssion_in(type, sdp) # Declare member variables here. Examples: # var a = 2 # var b = \"text\" var wsclient = WebSocketClient.new() var logged_in = true var username = \"\" var time_passed = 0 var worldlobby = Control var game = Control # Called when the node enters the scene tree for the first time. func _ready(): wsclient.connect(\"connection_closed\", self, \"on_connection_closed\") wsclient.connect(\"connection_error\", self, \"on_connection_error\") wsclient.connect(\"connection_established\", self, \"on_connection_established\") wsclient.connect(\"connection_failed\", self, \"on_connection_failed\") wsclient.connect(\"connection_succeeded\", self, \"on_connection_succeeded\") wsclient.connect(\"data_received\", self, \"on_data_received\") .connect(\"ws_out\", self, \"send\") func on_connection_closed(clean): print(\"connection closed \", clean) func on_connection_error(): print(\"connection error\") func on_connection_established(proto): print(\"connection established\", proto) func on_connection_failed(): print(\"connection failed\") func on_connection_succeeded(): print(\"connection succeeded\") func on_data_received(): var data=wsclient.get_peer(1).get_packet().get_string_from_utf8() print(\"data received:\", data) var msg = parse_json(data) if msg[\"mtype\"] == \"login-succeeded\": logged_in = true username = msg[\"payload\"] $menu/username.text = msg[\"payload\"] emit_signal(\"ws_login_in\", msg) elif msg[\"mtype\"] == \"login-needed\": logged_in = false elif msg[\"mtype\"] == \"candidate\": emit_signal(\"ws_candidate_in\", msg[\"payload\"]) elif msg[\"mtype\"] == \"session\": emit_signal(\"ws_session_in\", msg[\"payload\"]) elif msg[\"mtype\"] == \"world:list\": worldlobby.getWorlds(msg[\"payload\"]) elif msg[\"mtype\"] == \"game:event\": game.event(msg[\"payload\"]) elif msg[\"mtype\"] == \"game:get\": game.getGame(msg[\"payload\"]) $menu/received.text=$menu/received.text+data+\"\\n\" func send(data): print(\"sending\", data) wsclient.get_peer(1).put_packet(to_json(data).to_utf8()) func login(): var data = {\"mtype\": \"login\", \"payload\": str($menu/Connect/Username.text)+\"\\uffff\"+str($menu/Connect/Password.text)} print(\"sending \", data) send(data) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): wsclient.poll() if wsclient.get_connection_status() == wsclient.CONNECTION_CONNECTED: $menu/Connect.hide() $menu/Disconnect.show() if not logged_in: if time_passed >= 1: login() time_passed=0 else: time_passed = time_passed + delta $menu/connstatus.text=wsclient.get_connected_host() elif wsclient.get_connection_status() == wsclient.CONNECTION_CONNECTING: $menu/Connect.hide() $menu/Disconnect.hide() $menu/connstatus.text=\"connecting...\" else: $menu/Connect.show() $menu/Disconnect.hide() $menu/connstatus.text=\"not connected\" func _on_connectButton_pressed(): print(\"connecting to \"+\"ws://rc3.hacknplay.org:5000/ws\") var err = wsclient.connect_to_url(\"ws://rc3.hacknplay.org:5000/ws\") if err != 0: print(\"connection error \"+String(err)) func _on_disconnectButton_pressed(): wsclient.disconnect_from_host() func _on_sendButton_pressed(): var data = {\"mtype\": \"echo\", \"payload\": \"echo\"} wsclient.get_peer(1).put_packet(to_json(data).to_utf8()) func setWorldLobby(wl): worldlobby = wl func setGame(Game): game = Game " [sub_resource type="StyleBoxFlat" id=2] [node name="WSClient" type="Control"] anchor_right = 1.0 anchor_bottom = 1.0 margin_left = -2.3974 margin_top = 3.10672 margin_right = -712.397 margin_bottom = -247.893 script = SubResource( 1 ) __meta__ = { "_edit_use_anchors_": false } [node name="menu" type="Control" parent="."] margin_left = 274.0 margin_top = 237.0 margin_right = 314.0 margin_bottom = 277.0 __meta__ = { "_edit_use_anchors_": false } [node name="send-Button" type="Button" parent="menu"] anchor_right = 0.006 margin_left = -175.0 margin_top = -74.0 margin_right = -60.144 margin_bottom = -28.0 text = "echo" __meta__ = { "_edit_use_anchors_": false } [node name="connstatus" type="Label" parent="menu"] anchor_left = -6.828 anchor_top = -5.881 anchor_right = -6.828 anchor_bottom = -5.881 margin_left = 38.8798 margin_top = 19.2667 margin_right = 134.88 margin_bottom = 36.2667 text = "not connected" __meta__ = { "_edit_use_anchors_": false } [node name="exit" type="Button" parent="menu"] margin_top = -66.8752 margin_right = 30.0 margin_bottom = -32.8752 text = "X" __meta__ = { "_edit_use_anchors_": false } [node name="received" type="Label" parent="menu"] margin_left = -177.287 margin_top = -13.5705 margin_right = -57.2869 margin_bottom = 33.4295 custom_styles/normal = SubResource( 2 ) __meta__ = { "_edit_use_anchors_": false } [node name="username" type="Label" parent="menu"] margin_left = -127.673 margin_top = -214.973 margin_right = -11.6735 margin_bottom = -199.973 text = "not logged in" __meta__ = { "_edit_use_anchors_": false } [node name="Connect" type="Control" parent="menu"] margin_right = 40.0 margin_bottom = 40.0 __meta__ = { "_edit_use_anchors_": false } [node name="Username" type="LineEdit" parent="menu/Connect"] margin_left = -234.786 margin_top = -196.433 margin_right = -128.786 margin_bottom = -170.433 text = "Username" clear_button_enabled = true __meta__ = { "_edit_use_anchors_": false } [node name="Password" type="LineEdit" parent="menu/Connect"] margin_left = -236.327 margin_top = -168.766 margin_right = -130.327 margin_bottom = -142.766 text = "Password" secret = true clear_button_enabled = true __meta__ = { "_edit_use_anchors_": false } [node name="connect-Button" type="Button" parent="menu/Connect"] margin_left = -126.711 margin_top = -190.367 margin_right = -23.711 margin_bottom = -149.367 text = "connect" __meta__ = { "_edit_use_anchors_": false } [node name="Disconnect" type="Control" parent="menu"] margin_right = 40.0 margin_bottom = 40.0 [node name="disconnect-Button" type="Button" parent="menu/Disconnect"] anchor_right = 0.012 anchor_bottom = 0.017 margin_left = -184.168 margin_top = -188.46 margin_right = -73.4561 margin_bottom = -149.66 text = "disconnect" __meta__ = { "_edit_use_anchors_": false } [connection signal="pressed" from="menu/send-Button" to="." method="_on_sendButton_pressed"] [connection signal="pressed" from="menu/Connect/connect-Button" to="." method="_on_connectButton_pressed"] [connection signal="pressed" from="menu/Disconnect/disconnect-Button" to="." method="_on_disconnectButton_pressed"]