Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / How to get and receive messages from a websocket server

Author
Message
T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 20th Dec 2019 20:57 Edited at: 20th Dec 2019 21:00
Hi all,

Iam trying to receive messages from a websocket server to AppGameKit and back.

i have succesfull made a connection fom a websocket html file to a node server.js.
That works great.
But now i want to use AppGameKit sockets to connect to that same node server.js.
GetSocketConnected() gives a '1' back, but nothing happens on the server side or client side.

code server.js:




AGK client side code:



So the questions is: How can i send and receive data from and to a node js server?

i surely do something wrong or i miss some important code.

Any help regarding this is very appreciated.


edit: whoops, topic title should be: 'How to send and receive messages from a websocket server?'
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 21st Dec 2019 02:28 Edited at: 21st Dec 2019 06:56
Well, you need to build your message and send it all at once or flush the socket after your string and flush after integer or there is a high chance they will be received out or order by the server. But Id guess that your problem is that as soon as you send the message ur looking for a response for a billionth of a second then your incrementing your loop to 1 so it is no longer listening. and u will need a wait time in there of at least 400 ms to get a response from a sever even a local server may be over 30-40ms. but the real issue is is sending data from js to agk agk expects certain information because it is built for ease. The string must begin with a 4 byte length value followed by X bytes of string data, where X is the length value. The string should not be null terminated. If there is less than 4 bytes of data waiting to be read then this command will return immediately and return an empty string. You can check how much data there is to read by using GetSocketBytesAvailable. If there are 4 or more bytes waiting to be read then this command will wait until the entire string has been received before returning. also be careful connecting to a server with a socket connection an no safety in place. so u do not dos attack or overload a server. you can get into trouble for this legally.

T4r4ntul4
14
Years of Service
User Offline
Joined: 1st Jan 2010
Location: close to my pc
Posted: 21st Dec 2019 11:51
Hi,

Thank you for your reply.

Iam tottally new to websockets and multiplayer, i dont even know if i need 'ws' or 'socket.io' or totally something else.

Can you show me how to do it the right way?
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 22nd Dec 2019 00:59 Edited at: 22nd Dec 2019 01:10
Ive never actually interfaced with anything other than agk and sending out emails. Socket are universal from system to system pretty much so its a good place to start building a framework from. just the formatting for reading and sending is different from agk to endpoint. Im not 100% sure but i imagine if u send "123123123%" at the begging of the packet or some other information it should work then use the truncatestring(string(),%) command and anything after the % sigh can be grabbed and used. its a little weird but that would be my guess. generally u would send information in large quantities anyhow. tpc is handeled automatically so handshaking is done in the background and connection maintaining. so any data u want to send needs to be formatted anyhow. for instance the first infor i send from my game server looks like this


so your are probably sending to little data. among those 2 other problems i pointed out. average packet size on the web is about 72 characters

a standard communication is full usually pretty big.

nonom
6
Years of Service
User Offline
Joined: 12th Nov 2017
Location: Picking mushrooms
Posted: 8th Feb 2020 22:52 Edited at: 9th Feb 2020 17:41
I'm my case I'm using ReactPHP for the server side https://github.com/reactphp/socket

It's the echo example from https://github.com/reactphp/socket/blob/master/examples/01-echo-server.php


Your AppGameKit example to send and receive a custom "packet"


Some interesting links
https://github.com/reactphp/reactphp
https://sergeyzhuk.me/2017/06/22/reactphp-chat-server/
https://github.com/seregazhuk/reactphp-blog-series

Websockets async impl
https://github.com/ratchetphp/Ratchet

Login to post a reply

Server time is: 2024-04-24 19:54:12
Your offset time is: 2024-04-24 19:54:12