Bad websockets code help
Asked Answered
B

5

0

Hey guys. I'm attempting to make 2 scripts talk to each other via websocket in godot4, but I'm not sure how to do that- example projects seem to be using depreciated classes like WebsocketServer and WebsocketClient. I attempted to make something following the documentation but can't quite get it to work.

Client code is straight copied from documentation:

extends Node

var socket = WebSocketPeer.new()


func _ready():
	socket.connect_to_url("wss://localhost:8099")

func _process(_delta):
	socket.poll()
	#print(socket.get_connected_host())
	var state = socket.get_ready_state()
	if state == WebSocketPeer.STATE_OPEN:
		while socket.get_available_packet_count():
			print("Packet: ", socket.get_packet())
	elif state == WebSocketPeer.STATE_CLOSING:
		# Keep polling to achieve proper close.
		pass
	elif state == WebSocketPeer.STATE_CLOSED:
		var code = socket.get_close_code()
		var reason = socket.get_close_reason()
		print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1])
		set_process(false) # Stop processing.

Server code is based on TCPServer(since it's actually capable of listening) using WebsocketPeer as remote peer as suggested by documentation(documentation also recommends looking up online tutorial but I don't think that one is out yet):

extends Node


var server= TCPServer.new()
var connector
var sender= WebSocketPeer.new()

func _ready():
	server= TCPServer.new()
	print(server.listen(8099))
	print(server.get_local_port())
	print(server.is_connection_available())


func _process(_delta):
	var conn= server.is_connection_available()
	if conn:
		if !connector:
			connector= server.take_connection()
			connector.set_no_delay(true)

			sender.accept_stream(connector)#this supposedly performs the handshake according to documentation?
			print(connector.get_status())
			while sender.get_ready_state()!= WebSocketPeer.STATE_OPEN:
				sender.poll()

			# Once the connection is opensend a packet
			sender.put_packet([1,2,"aaa"])

Print outputs in order are:
0
8099
false
2
So that part works fine.
The problem is sender.get_ready_state() of the client never changes from 0(CONNECTING) to 1(OPEN) and I can't send packets.
I'd be grateful for any tips/examples of how this actually should work

Brebner answered 12/5, 2023 at 23:37 Comment(0)
S
0

(Sorry for the late answer)
I used your some of your code for my own socket server, and I think I've found the issue. Changing the url with which your client connects to "ws://localhos:8099" should do the trick. Apparently wss doesn't work when connecting directly to IP adresses, including localhost.

Speechless answered 8/6, 2023 at 18:4 Comment(0)
B
0

Speechless Thanks for the answer! Does this code work on your server? I did change from wss to ws, but couldn't observe difference in behavior. Still stuck connecting

Brebner answered 8/6, 2023 at 21:36 Comment(0)
S
0

I couldn't get your code to run, but I've made a new framework made based on your design:

Client side:

extends Node

var socket = WebSocketPeer.new()
var url = "ws://localhost:8080"

func _ready():
	socket.connect_to_url(url)

func _process(delta):
	socket.poll()
	var state = socket.get_ready_state()
	if state == WebSocketPeer.STATE_OPEN:
		if socket.get_available_packet_count():
			var msg = socket.get_packet().get_string_from_utf8()
			print(msg)
	elif state == WebSocketPeer.STATE_CLOSING:
		pass
	elif state == WebSocketPeer.STATE_CLOSED:
		socket.connect_to_url(url)

Server side:

extends Node

var server = TCPServer.new()
var socket = WebSocketPeer.new()
var stream : StreamPeerTCP
var PORT = 8080

func _ready():
	server.listen(PORT)

func _process(delta):
	var conn = server.is_connection_available()
	if conn and !stream:
		stream = server.take_connection()
		stream.set_no_delay(true)
		socket.accept_stream(stream)
		
		while socket.get_ready_state() != WebSocketPeer.STATE_OPEN:
			socket.poll()
		
		socket.send_text("Test message from server")

I tested it on my computer and it works as intended. Let me know if it works for you too

Speechless answered 9/6, 2023 at 19:9 Comment(0)
L
0

Speechless For me your code dont work, it never open the port ...
The server.is_connection_available() is never true, so there is no way to know if the server opened or not.

Lanza answered 23/8, 2023 at 14:19 Comment(0)
P
0

Has anybody managed to convert a Godot 3.x websocketServer implementation to Godot 4.x? I have a Godot 3.x application that communicates using websockets with web browser clients and Im trying to get this working with Godot 4.x

Paduasoy answered 6/1 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.