0 votes

Hi there,
I have a basic script to connect to a web-socket. I successfully connect to the socket & getpacket() function works - however putpacket() is returning 0 every time and the server doesn't seem to be picking it up. Any ideas why getpacket is working, but putpacket isn't? Thanks for checking this out! Appreciate any thoughts.

below is the code:

extends Node

export var websocket_url = "ws://localhost:8080"
var _client = WebSocketClient.new()

func _ready():
_client.connect("connection_closed", self, "_closed")
_client.connect("connection_error", self, "_closed")
_client.connect("connection_established", self, "_connected")
_client.connect("data_received", self, "_on_data")

var err = _client.connect_to_url(websocket_url)
if err != OK:
    print("Unable to connect")
    $Label.text = "Unable to connect"
    set_process(false)

func _closed(was_clean = false):
print("Closed, clean: ", was_clean)
$Label.text = "Closed, clean: " + was_clean
set_process(false)

func _connected(proto = ""):
print("Connected with protocol: ", proto)
$Label.text = "Connected with protocol: " + proto
_client.get_peer(1).put_packet("Test packet".to_utf8())

func _on_data():
var a = _client.get_peer(1).get_packet().get_string_from_utf8()
print("Got data from server: ", _client.get_peer(1).get_packet().get_string_from_utf8())

func _process(delta):
_client.poll()
in Engine by (16 points)

2 Answers

0 votes

So I found the answer: I asked a friend and he said the put_packet() function send's BINARY to the server side. I had to program that on the server side to convert the Binary to UTF-8.

by (16 points)
0 votes

You need to set the peer's write mode to Text.

You can do this with:

_client.get_peer(1).set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
by (20 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.