Moving to Websocket client/server model
This commit is contained in:
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_)
|
||||
Reference in New Issue
Block a user