Page 1 of 1

ReceiveData callback and userdata

Posted: Sun Apr 02, 2017 7:55 pm
by msu
Hi,
How can I use callback and userdata with ReceiveData(1, #RECEIVEALL,..)?
An example would be nice.

Greetings, Michael

Re: ReceiveData callback and userdata

Posted: Tue Apr 04, 2017 9:20 pm
by msu
Quick support here! :(

Re: ReceiveData callback and userdata

Posted: Tue Apr 04, 2017 10:53 pm
by xabierpayet
here you have a client and a server example

Server:

CreateServer(1,2900)
conexions=0
Function p_ConnectFunc(msg)
Switch(msg.action)
Case "OnDisconnect":
closeconnection(msg.id)
conexions=conexions-1
Case "OnConnect":
conexions=conexions+1
conexioentrant=msg.clientid
Case "OnReceiveData":
data$ = ReceiveData(msg.id, #RECEIVELINE)
senddata(msg.id,data$)
EndSwitch
EndFunction
InstallEventHandler({OnConnect = p_ConnectFunc,OnDisconnect = p_ConnectFunc, onreceivedata=p_connectfunc})
BeginDoubleBuffer()
repeat
cls
checkevent
locate(0,0)
print("conexions:",conexions)
IF CONEXIONS>0 THEN senddata(CONEXIOENTRANT,"PP")
Flip()
forever

and here is the client:

serv$="192.169.1.123"
port=2550
OpenConnection(a, serv$,2900)
nprint("leftmouse to start")
WaitLeftMouse()
while(0=0)
senddata(1,"pp")
wend

Re: ReceiveData callback and userdata

Posted: Tue Apr 04, 2017 10:54 pm
by airsoftsoftwair
Probably too easy a question for the folks on here because it's pretty much all said in the documentation :) Well, just do something like this:

Code: Select all

Function p_DumpFunc(msg)
   DebugPrint(msg.count, msg.total, msg.userdata)
   Return(False)
EndFunction

ReceiveData(1, #RECEIVEALL, True, p_DumpFunc, "Hello msu!")
This code will receive all data until the sender closes its connection. The callback will print how many bytes it has just received and the total byte count. It will also print "Hello msu!" because that is the userdata. Note that ReceiveData()'s syntax is different for other types than #RECEIVEALL. But it's all in the documentation... which will also be available in German really soon :)