Moving to Websocket client/server model
This commit is contained in:
		
							parent
							
								
									f6c3594d50
								
							
						
					
					
						commit
						a6adb8b011
					
				
							
								
								
									
										22
									
								
								Scenes/Lobby.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								Scenes/Lobby.tscn
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| [gd_scene load_steps=2 format=2] | ||||
| 
 | ||||
| [ext_resource path="res://Scripts/Lobby.gd" type="Script" id=1] | ||||
| 
 | ||||
| [node name="Lobby" type="Control"] | ||||
| anchor_right = 1.0 | ||||
| anchor_bottom = 1.0 | ||||
| script = ExtResource( 1 ) | ||||
| __meta__ = { | ||||
| "_edit_use_anchors_": false | ||||
| } | ||||
| 
 | ||||
| [node name="ItemList" type="ItemList" parent="."] | ||||
| anchor_left = 0.0449219 | ||||
| anchor_top = 0.125 | ||||
| anchor_right = 0.254477 | ||||
| anchor_bottom = 0.955 | ||||
| margin_right = -27.584 | ||||
| margin_bottom = -62.0 | ||||
| __meta__ = { | ||||
| "_edit_use_anchors_": true | ||||
| } | ||||
							
								
								
									
										14
									
								
								Scripts/Lobby.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								Scripts/Lobby.gd
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| extends Control | ||||
| 
 | ||||
| func _init(): | ||||
| 	WebsocketController.connect("hostjoin", self, "_on_hostjoin") | ||||
| 
 | ||||
| func _ready(): | ||||
| 	WebsocketController.connect_websocket() | ||||
| 	 | ||||
| func _on_hostjoin(): | ||||
| 	var location = JavaScript.get_interface("location") | ||||
| 	if location and location.hash != "": | ||||
| 		WebsocketController.join_game(location.hash) | ||||
| 	else: | ||||
| 		WebsocketController.host_game() | ||||
| @ -1,7 +1,14 @@ | ||||
| extends Control | ||||
| 
 | ||||
| onready var Location = JavaScript.get_interface("location") | ||||
| 
 | ||||
| func _ready(): | ||||
| 	if Location and Location.hash != "": | ||||
| 		## TODO | ||||
| 		# join_room(location.hash) | ||||
| 		pass | ||||
| 	$Buttons/Host.connect("pressed", self, "host_pressed") | ||||
| 
 | ||||
| func host_pressed(): | ||||
| 	get_tree().change_scene("res://Scenes/Main.tscn") | ||||
| 	if Location: Location.hash = "#blarg" | ||||
| 	get_tree().change_scene("res://Scenes/Lobby.tscn") | ||||
|  | ||||
							
								
								
									
										38
									
								
								Scripts/WebsocketController.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								Scripts/WebsocketController.gd
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | ||||
| extends Node | ||||
| 
 | ||||
| var client = WebSocketClient.new() | ||||
| var peer | ||||
| 
 | ||||
| signal hostjoin | ||||
| 
 | ||||
| func _init(): | ||||
| 	client.connect("connection_established", self, "_on_connect") | ||||
| 	client.connect("data_received", self, "_on_data") | ||||
| 	 | ||||
| func _on_connect(_proto): | ||||
| 	peer = client.get_peer(1) | ||||
| 	peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) | ||||
| 	 | ||||
| func _on_data(): | ||||
| 	var msg = peer.get_packet().get_string_from_utf8() | ||||
| 	var regex = RegEx.new() | ||||
| 	regex.compile("([\\w_]+):") | ||||
| 	var res = regex.search(msg) | ||||
| 	match res.strings[1]: | ||||
| 		"HOSTJOIN": | ||||
| 			emit_signal("hostjoin") | ||||
| 	 | ||||
| func _process(_delta): | ||||
| 	client.poll() | ||||
| 	 | ||||
| func send_message(message): | ||||
| 	peer.put_packet(message.to_utf8()) | ||||
| 	 | ||||
| func connect_websocket(): | ||||
| 	client.connect_to_url(ProjectSettings["global/server_url"]); | ||||
| 
 | ||||
| func host_game(): | ||||
| 	send_message("HOST:") | ||||
| 	 | ||||
| func join_game(hash_): | ||||
| 	send_message("JOIN: %s" % hash_) | ||||
| @ -33,16 +33,17 @@ _global_script_class_icons={ | ||||
| [application] | ||||
| 
 | ||||
| config/name="Hexland" | ||||
| run/main_scene="res://Scenes/MainMenu.tscn" | ||||
| run/main_scene="res://Scenes/Lobby.tscn" | ||||
| config/icon="res://icon.png" | ||||
| 
 | ||||
| [autoload] | ||||
| 
 | ||||
| WebsocketController="*res://Scripts/WebsocketController.gd" | ||||
| Debug="*res://Scripts/Debug.gd" | ||||
| 
 | ||||
| [global] | ||||
| 
 | ||||
| main=false | ||||
| server_url="127.0.0.1:8080" | ||||
| 
 | ||||
| [gui] | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user