Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Successfully connects, but only after ~60 seconds. #5

Open
mkgiga opened this issue Dec 26, 2024 · 0 comments
Open

Successfully connects, but only after ~60 seconds. #5

mkgiga opened this issue Dec 26, 2024 · 0 comments

Comments

@mkgiga
Copy link

mkgiga commented Dec 26, 2024

It seems that it works perfectly fine in the browser during remote debug or web export,
but when you run your game in the editor, it doesn't connect-- at least not immediately. You have to wait for around a minute, and only then does my server receive the connection and fire its connection event

I'm using express.js+socket.io 4.8.1

my client code (using most of the example in the repo)

class_name NetworkClient extends Node

var client = SocketIOClient
var backendURL: String

@onready var game: Node3D = get_node("../Game")

func _ready():
	print("Test") # this fires immediately when the game boots up
	# so it's not like the scene tree loads late
	
	# prepare URL
	backendURL = "http://localhost:3000/game"

	# initialize client
	client = SocketIOClient.new(backendURL);

	# this signal is emitted when the socket is ready to connect
	client.on_engine_connected.connect(on_socket_ready)

	# this signal is emitted when socketio server is connected
	client.on_connect.connect(on_socket_connect)

	# this signal is emitted when socketio server sends a message
	client.on_event.connect(on_socket_event)

	# add client to tree to start websocket
	add_child(client)

func _exit_tree():
	# optional: disconnect from socketio server
	client.socketio_disconnect()

func on_socket_ready(_sid: String):
	# connect to socketio server when engine.io connection is ready
	print("Socket ready")
	client.socketio_connect()
	

func on_socket_connect(_payload: Variant, _name_space, error: bool):
	if error:
		push_error("Failed to connect to backend!")
	else:
		print("Socket connected")

func on_socket_event(event_name: String, data: Variant, _name_space):
	match(event_name):
		"chunks":
			PacketHandler.chunks(data)

func send(event: String, data: Variant):
	client.socketio_send(event, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant