How to get debug information for tsNet transfers
Introduction
It can often be useful when building network applications to receive extra information on what is taking place while any network requests are made.
tsNet provides you the ability to retrieve detailed information about the communication that is occurring between your application and any network server.
Lay out the Stack
Create a new stack and drag three buttons onto it.
For the first button, set the label for it to "Enable Debug". Name the last two buttons "tsNetGetSync" and "tsNetGet" respectively.
Now add two fields to your stack. Name the first field "Status Message" and the second one "Debug Messages".
Save this stack.
Create the Script
Edit the script of the button that you labelled "Enable Debug" by right clicking on the button and selecting "edit script".
This button will configure tsNet to send debug messages to a handler called updateDebugField.
Add the following code.
on mouseUp
put empty into field "Debug Messages"
if the label of me is "Enable Debug" then
tsNetSetDebugCallback("updateDebugField")
set the label of me to "Disable Debug"
else
tsNetSetDebugCallback("")
set the label of me to "Enable Debug"
end if
end mouseUp
on updateDebugField pConnId, pMessage
put pConnId & ":" && pMessage after field "Debug Messages"
put the formattedHeight of field "Debug Messages" into tScroll
set the vScroll of field "Debug Messages" to tScroll
end updateDebugField
Now edit the script of the button named "tsNetGetSync". This button will perform a simple synchronous HTTP GET request.
Add the following code.
on mouseUp
put "Starting transfer" into field "Status Message"
put tsNetGetSync("http://www.livecode.com",tHeaders,tOutHeaders,tResult,tBytes) into tData
if tResult is 200 then
put "Transfer complete" into field "Status Message"
else
put "Transfer failed with server status code:" && tResult into field "Status Message"
end if
end mouseUp
Lastly, there is one more button that needs some code! Edit the script of the button named "tsNetGet". This button will perform an asynchronous HTTP GET request.
Add the following code.
on mouseUp
put "Starting transfer" into field "Status Message"
put tsNetGet("My Connection ID","http://www.livecode.com",tHeaders,"transferComplete") into tResult
if tResult is not empty then
put "Transfer failed:" && tResult into field "Status Message"
end if
end mouseUp
on transferComplete pID, pResult, pBytes, pCurlCode
if pCurlCode is 0 and pResult = 200 then
put "Transfer complete" into field "Status Message"
else
put "Transfer failed with curl code:" && pCurlCode && "and server status code:" && pResult into field "Status Message"
end if
tsNetCloseConn pID
end transferComplete
Test
Now switch to Run mode and click the "Enable Debug" button to tell tsNet to display debug information when a network request is made.
Next click the “tsNetGet” button and you will see some debug information appear in the bottom field.
You will notice that each line of the debug displayed in the field begins with "My Connection ID".
The connection identifier specified as the first parameter to the tsNetGet function is passed as the first parameter to the debug handler that you set (in this case to the updateDebugField handler).
When using asynchronous transfers, the connection identifier is always used to determine which connection is being referred to.
Now click the "tsNetGetSync" button and you will see the new debug appear for the synchronous request in progress.
This time you will see that the start of each line contains the URL that the request is being made to. The connection identifier will always be set to the URL being requested when you are using synchronous transfers,
0 Comments
Add your comment