Async request in OnRaiseEvent

hanjung
hanjung
edited June 2020 in Photon Server
Hi, I'm trying to make a async web request to post json and I find the example a little bit confusing
https://doc.photonengine.com/en-us/server/current/plugins/manual#outbound_http_calls


In the following example, Async flag is set with WebFlags.ShouldSendSync(), how is it different from just async = true (for async request) and async = false (for non async request)?
public override void OnRaiseEvent(IRaiseEventCallInfo info)
{
    HttpRequest request = new HttpRequest()
    {
        Callback = OnHttpResponse,
        Url = "https://requestb.in/<token>", // change URL
        Async = !WebFlags.ShouldSendSync(info.Request.WebFlags),
        UserState = info
    };
    // here you can modify the request to suit your needs
    PluginHost.HttpRequest(request);
    info.Defer();
}


Next example - I'm assuming this is non async request since Async flag is not set
What happens if I pass a Async flag
Async = WebFlags.ShouldSendSync(info.Request.WebFlags) or Async = false
Will it still make non async request?
void PostJson(string url, HttpRequestCallback callback, string json)
{
    var stream = new MemoryStream();
    var data = Encoding.UTF8.GetBytes(json);
    stream.Write(data, 0, data.Length);
    HttpRequest request = new HttpRequest()
    {
        Callback = callback,
        Url = url,
        DataStream = stream,
        Method = "POST",
        ContentType = "application/json"
    };
    // here you can modify the request to suit your needs
    PluginHost.HttpRequest(request);
}

Ultimately I want to have a method that PostJson and be able make either async or non async by passing a parameter

Comments

  • hi, @hanjung

    I'm very sorry but I can not understand what you want to get. I mean I understand you final question, but do not understand all what is above

    you probably found next code in our WebHooksPlugin:
    ```
    callAsync: !WebFlags.ShouldSendSync(info.Request.WebFlags));
    ```
    it will send Sync http request in case if flag is set in RaiseEvent Request, and will send Async request if flag is not set

    that is it

    Please point me to what you do not understand.

    best,
    ilya
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @hanjung,

    I want to add a few things to my colleague @chvetsov's answer:

    You can completely ignore and not use WebFlags.ShouldSendSync.
    It is on the documentation page of plugins as an example and it's meant to show you that it is possible to decide from the client if a HTTP request from a plugin that is a result of a RaiseEvent or SetProperties should be sent async or not.

    You can skip it altogether and simply set the HttpRequest.Async to true or false.

    We recommend migrating to or starting with Photon v5.

    Sorry for the confusion.