libUrl tsNet and socketTimeoutInterval

Sannyasin Brahmanathaswami brahma at
Sun Dec 10 14:46:43 EST 2017

@ Charles

It would appear that perhaps there is a possible global approach that could cover a wide variety of net access conditions.

am I being naïve or could it be this simple?

if we just have this running and available all the time

tsNetSetTimeouts… [add your params here] 

tsNetSetStatusCallback "urlCallProgressUpdate"

on urlCallProgressUpdate
  # check the status of any and all URL download operations
  # give user feedback in case connection drops or transfers speed drops below low limit
end urlCallProgressUpdate

maybe that's really all that is needed. Then we don't worry about or code any kind of status monitoring into specific URL operations.

One could "dynamically" change the time outs if different scenarios called for allowing longer time spans, i.e users expect a wait while downloading a "package" but they the don't when loading a web page.

we might simply have this in back script for the whole app

command toggleNetMonitor  pBool
  if pBool = "true" then
      tsNetSetStatusCallback urlCallProgressUpdate
    tsNetSetStatusCallback ""  
  end if
end   toggleNetMonitor 

I don't know if there is any overhead to leaving this call back be live "24-7-365" in the app or not… but this way we could turn it on and off if that is better practice.


On 12/8/17, 5:08 PM, "use-livecode on behalf of Sannyasin Brahmanathaswami via use-livecode" <use-livecode-bounces at on behalf of use-livecode at> wrote:

    Ralph -- I sent this to you off list by mistake
    copying here FWIW if anyone is lurking. This all looks great and covers "download files" use cases
    The remaining puzzles are one off url calls from/for mobile controls :
    1)  remote path to audio.mp3; for player
    2) url web calls in browser widget to
         a) web pages
         b) youtube (video streaming)
    and how these play with TSNet, bandwidth dropping below set limits, or dropped connections.
    As yet mysterious….
    " <rdimola at> wrote:
        I async also works in iOS just fine and dandy. In My app I am getting the background images for cards while the user first logs in. Async allows the images to be downloaded in the background. This allows me to change images without releasing a new app without hanging the app up when I used blocking libURL. The implementation was so easy it was frightening. I also have 6 of these queued up and running at the same time!
        First is a utility(NetworkType) to check the type of networking and a tsNet initialization handler (EISinitTSnet):
        function GetNetworkType
           local tLibUrlDriver
              put the behavior of stack"revLibUrl"into tLibUrlDriver
           end try
           if tLibUrlDriver is empty then
              return "Sockets"
              return "tsNet"
           end if
        end GetNetworkType
        command NetworkType
           if GetNetworkType() = "Sockets" then
              answer"tsNet is disabled"
              answer "tsNet in use"&cr&"Version==>"& tsNetVersion()
           end if
        end NetworkType
        command EISinitTSnet
           local tResult
              put the result into tResult
              if tResult is not empty then
                 if tResult = "tsneterr: Already initialised" then
                    answer tResult
                 end if
                 tsNetSetTimeouts 60, 5000, 5000, 5000, 5, 10000
              end if
           catch someerror
              if IsInternalUser(true) then answer TranslateError(someerror)
           end try
        end EISinitTSnet
        The Get_Path function below is a function returns paths that are transparent between IDE and mobile. (Richard G. I got sucked into the returning the trailing slash before I realized it was not the best way to do it). I hacked out this code so there could be an error but the basic idea is here.
        I call this first handler(StartBackgroundImageDownload) 12 time quickly and all the downloads work. The app is fully functional while the downloads are in progress. I use the filename as the pID. This makes things easy when the download completes. I have not tested timeouts yet. That's the next step, handling the in/out of network access/slow network speed. Standby....
        This is the guts of the asysnc file downloading:
        command StartBackgroundImageDownload pImageName
           local pHeaders, tStatus
           put tsNetGetFile(pImageName, Get_Path("Updates")&pImageName,  \
                 ""& \
                 urlencode(pImageName), pHeaders, "DownLoadBackgroundImageComplete") into  tStatus-- start download
           if tStatus is not empty then
                 answer tStatus
                 end if
        end StartBackgroundImageDownload
        on DownLoadBackgroundImageComplete pID, pResult, pBytes, pCurlCode
           local tData, tHeaders
           --answer "File==>"&pID&cr&"Bytes==>"&pBytes&cr&"Result==>"&pResult&cr&"CurlCode==>"&pCurlCode
           if pCurlCode is not 0 then
                 answer tsNetRetrError(pID)
              if pResult = 404 then
                 answer "File'"&&pID&&"not found==>"&pBytes&&"Result==>"&pResult
                 delete file Get_Path("Updates")&pID -- delete file with 404 error in it
                 --answer "File==>"&pID&cr&"Bytes==>"&pBytes&cr&"Result==>"&pResult&cr&"CurlCode==>"&pCurlCode
                 if pBytes > 2000 then -- GOOD DOWNLOAD
                    rename file (Get_Path("Updates")&pID) to (Get_Path("BackgroundImages")&pID) -- file download complete
                       --answer "File'"&&pID&&"has been downloaded. Result==>"&pResult
                       answer "File'"&&pID&&"has been downloaded but is too small==>"&pBytes&&"Result==>"&pResult -- this was before I had the 404 check
                       delete file Get_Path("Updates")&pID
                 end if
              end if
           end if
           tsNetCloseConn pID
        end DownLoadBackgroundImageComplete
        Ralph DiMola
        IT Director
        Evergreen Information Services
        rdimola at
        -----Original Message-----
        From: use-livecode [mailto:use-livecode-bounces at] On Behalf Of Sannyasin Brahmanathaswami via use-livecode
        Sent: Friday, December 08, 2017 2:21 PM
        To: How to use LiveCode
        Cc: Sannyasin Brahmanathaswami
        Subject: Re: libUrl tsNet and socketTimeoutInterval
        Awesome Ralph:
        Looks like we need to crowd fund Charles for documentation (smile)
        You are about 3 days ahead of me… can you post some code snippets  of your methods?
        On 12/7/17, 1:54 PM, "use-livecode on behalf of Ralph DiMola via use-livecode" <use-livecode-bounces at on behalf of use-livecode at> wrote:
            Ok, I just got async tsNet working in IDE and Android(Testing iOS later). I works great! Hat tip to all the contributors. One question and one Observation:
            1) How do you query the current tsNet timeout settings? I have quite a few places where I save the socketTimeoutInterval, set it to a new value, do something and then set it back to the saved timeout. I don't see a way to query this in the dictionary.
            2) I seems the you don't need to do a "tsNetInit" in the IDE. In the IDE one gets "tsneterr: Already initialized". On Android this error does not occur.
            Ralph DiMola
            IT Director
            Evergreen Information Services
            rdimola at
        use-livecode mailing list
        use-livecode at
        Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
    use-livecode mailing list
    use-livecode at
    Please visit this url to subscribe, unsubscribe and manage your subscription preferences:

More information about the Use-livecode mailing list