Socket Subclass through Thread

  1. 2 months ago

    Hello Everyone,
    I am using Xojo.Net.HttpSocket in order to Communicate with a Json API.
    I have created a subclass of Xojo.Net.HTTPSocket named fUpdate that implements all the Event Handlers (PageReceived,HeadersReceived)
    The code in these Events just writes a text in the log file of the app and also has break.

    I call this Socket using a subclass of a Thread named thUpdate that has the following code in its Run Event:

    Dim info as new xojo.Core.Dictionary

    ivSocket = new ForkyUpdate
    ivSocket.id = id

    select case state
    case 0
    info.Value("status") = "cancelled"
    case 1
    info.Value("status") = "downloaded"
    case 2
    info.Value("status") = "printed"
    case 3
    info.Value("status") = "assigned"
    case 4
    info.Value("status") = "delivered"
    end select
    Dim json as text
    json = Xojo.Data.GenerateJSON(info)
    Dim data as xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(json)

    ivSocket.SetRequestContent(data,"application/json")

    Dim lvS as String ="AUTHENTICATIONSCRIPTISHERE"
    ivSocket.RequestHeader("Authorization") = lvS.ToText
    ivSocket.RequestHeader("Content-type") = "application/json"

    ivSocket.Send("PATCH",App.forkyUrl.ToText+id.toString.ToText) //DEV

    With this way I never receive anything in the Socket Events.
    If I add the Socket in my Window and call the same method without the use of a thread i receive the events but when I try to use the Socket of
    the window's controls more than i one time i receive the error : a request is already in progress.

    I must have a check in the event in order to see that the API answer me correctly or not in order to deal with it.

    Do you have any suggestions? Is there something am I doing wrong?

    Thank you in advance.

    Change the Thread to timer with 2-3 seconds and check it..

  2. Change the Thread to timer with 2-3 seconds and check it..

  3. Greg O

    Jul 11 Xojo Inc Somewhere near Raleigh, NC

    @SebastianKapellas but when I try to use the Socket of
    the window's controls more than i one time i receive the error : a request is already in progress.

    If you’re trying to start another request from the PageReceived event or a method that’s called as a result of it, that would cause this. The socket isn’t truly free for reuse yet at this point. Usually the solution is to use Xojo.Core.Timer.CallLater to call a method shortly after the event completes.

  4. Thank you both. I added a Timer in the Window with a period of 50 ms that call the Socket and now i receive the answer in the Socket events.

    But i still cannot understand why the Thread cannot handle the response. Is it getting out of scope?

  5. Greg O

    Jul 11 Xojo Inc Somewhere near Raleigh, NC
    Edited 2 months ago

    @SebastianKapellas Thank you both. I added a Timer in the Window with a period of 50 ms that call the Socket and now i receive the answer in the Socket events.

    But i still cannot understand why the Thread cannot handle the response. Is it getting out of scope?

    I believe that’s a known bug. Sockets don’t operate on threads anyway. Their events always fire on the main thread.

or Sign Up to reply!