Asynchronous HTTPSocket with timeout

Hi All,

I’m new to Xojo and this is my new post. If I make any mistakes I apologies in advance hope you bare with me.

I’m having an issue in an application I’m working on. First I must say that this is written in Realbasic (before it was renamed to xojo) Real Studio 2011 Release 2

My application is an image uploader and it uses HTTPSockets to submit the images to the server.

//I build a form here
//some code
data = data + "--" + boundary + nl

//some code
HTTPSocketUploadImage.SetPostContent data, "multipart/form-data, boundary=" + boundary

//and post it here
HTTPSocketUploadImage.Post(url)

the problem with this is if the server does not rerun a reply it waits indefinitely for a reply and the program does nothing after that. The reason I need the asynchronous posts is that I’m using three of these in parallel to speed things up. On the other hand if I don’t use asynchronous the UI becomes unresponsive too.

Is there a work around this?

please refer to this image for a better explanation of my issue

just in case above image tag does not work
You can find it here

Thanks for your time. Any help is much appreciated.

If you want the UI to stay responsive you can use HTTPSocket.Yield = True .
Also, use threads to run many processes in parallel, instead of having 3 different HTTPSocket objects.

A question, do you handle the events of the HTTPSocket?

Maybe the HTTPSocket object goes out of scope, if it is a local variable.

It sounds like the server may be assuming an http 1.1 persistent connection. Try setting a header:

connection: close

You might also want to implement the ReceivedBytes event and see if there is data coming in, but slowly.

Another idea though… You might want to just try your app in Xojo 2014r2.1, just to see if this is a bug that has been fixed in a later version.

Thanks for all replies. Greatly appreciate it.

@Ashot: I have already tried HTTPSocket.Yield = True but was not giving me the expected result. May be I will give it another try. I agree with you about the threads but this was written by another developer and I’m trying to solve the issues it’s having. May be it’s worth improving this.
@Greg: I have tried ‘received bytes’ but the issues is it will not fire that event if you don’t get a response from the server you are posting to and the socket keeps listening (as per my understanding) . I will try HTTPSocket.Yield = True again and keep you guys posted.

If nothing else works I will probably try the upgrade.

** Another question does anybody know how to trigger a timeout for HTTPSocket? I mean a fake a timeout? I’m not sure if this is a silly question but I don’t know how to do it.

Thanks for the help again
Cheers

A timer
You could subclass HTTPSocket to add your own properties - one of which is a timer + methods to use for AddHandler.

Then when you use your httpsocket subclass you start the timer when a request is made and when it’s answered you stop the timer otherwise when it’s action event is run you stop it and raise a “timeout” error (however is appropriate for your application)

I’m new to Real Basic / Xojo and have inherited a project from someone else. Some of what people are suggesting sounds great but I’m not sure where to start
e.g. @Normans idea of subclass and adding a handler.
Would anyone be able to give an example or point me to right direction?

creating a subclass of anything is really quite easy

Use the Insert Menu and Insert a Class
Select your new class in the navigator
Show the inspector
In the Super field enter “HTTPSocket”
You now have a subclass of HTTP Socket with whatever name you want
It doesn’t DO anything more than the basic http socket
Now you can add properties & methods to it to customize how it behaves (like I mentioned)

It’s worth trying to do this in a small project before doing it in the project you inherited
Also make sure you go through the various books available on http://documentation.xojo.com
See the section User Guide, Upgrade Guide, QuickStarts and Tutorials