0 votes

Example to connect:

func connectToServer(ip, port):
    client = StreamPeerTCP.new()
    client.connect(ip, port)  

Then in my update, I use: (this might be where my problem is)

if client.is_connected() && client.get_available_bytes() >0:
    print(str(client.get_string(client.get_available_bytes())))

Now, I'm running a basic TCP Server in nodejs. If I do:

socket.write('PATCH 1.10')
socket.write('PATCH 1.10')
socket.write('PATCH 1.10')
socket.write('PATCH 1.10')
socket.write('PATCH 1.10')
socket.write('PATCH 1.10')
socket.write('PATCH 1.10')
socket.write('PATCH 1.10')

It actually doesn't send as individual messages, so when I get my string from get_string (above) I receive:

enter image description here

Is there a way to make it so each time a message (packet) is sent, they come as individual packets instead of added to the TCP Stream? Thanks for reading!

in Engine by (313 points)

Sorry folks, looks like this is just how TCP Works (Stream Based)

http://stackoverflow.com/questions/13279754/nodejs-tcp-server-ondata-small-chunks

I don't think it's Godot's fault. I'm just used to my WebSocket server :(

More info here: http://stackoverflow.com/questions/17446491/tcp-stream-vs-udp-message

1 Answer

+2 votes
Best answer

This is part of the way TCP works -- it is stream-based, not packet-based, unlike UDP.

The simplest way to do what would want, though, would be to pick some "message delimiter character", like \n or even \0, and then append all bytes into an array until you meet it. Then you would process the next message.

A more involved way to solve it would be to encode all messages as JSON, and then (somehow) monitor when the JSON structure ends, and the next begins.

by (1,608 points)
selected by

@Boljidar, I did the \n delimited approach, works awesome! Thank you!

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 Frequently asked questions and 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 [email protected] with your username.