Search Unity

UnityWebRequest Not Working on WebGL Build

Discussion in 'Web' started by Cookn5, Mar 27, 2019.

  1. Cookn5

    Cookn5

    Joined:
    Jun 27, 2018
    Posts:
    2
    Hello,

    I am currently working on a WebGL game that allows users to send SMS messages using Twilio's REST API. My scripting runtime version is .NET 4x Equivalent and the API compatibility level is .NET Standard 2.0. Twilio offers a C# helper library available through Nuget. I was able to manually import all the required Twilio .NET Standard 2.0 dll (and all the dependency dlls) and have the following code which works in the editor, but gives an error associated with threading.
    Code (CSharp):
    1.         const string accountSid = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    2.         const string authToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    3.  
    4.         TwilioClient.Init(accountSid, authToken);
    5.  
    6.         var message = MessageResource.Create(
    7.         from: new Twilio.Types.PhoneNumber("+1360900XXXX"),
    8.         body: "WebGL Test",
    9.         to: new Twilio.Types.PhoneNumber("+1540453XXXX")
    10.         );
    After a lot of research, I noticed that WebGl does not support multiple threads so I tried the following approach using a Coroutine and UnityWebRequests:
    Code (CSharp):
    1.     public void ButtonClicked()
    2.     {
    3.  
    4.         string url = "api.twilio.com/2010-04-01/Accounts/";
    5.         string service = "/Messages.json";
    6.         string account_sid = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    7.         string auth = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    8.         string credentials = "https://" + account_sid + ":" + auth + "@" + url + account_sid + service;
    9.  
    10.         StartCoroutine(PostRequest(credentials));
    11.     }
    12.  
    13.     IEnumerator PostRequest(string url)
    14.     {
    15.         WWWForm form = new WWWForm();
    16.         form.AddField("To", "+1360XXXXXXX");
    17.         form.AddField("From", "+1360XXXXXXX");
    18.         form.AddField("Body", "WEBGL TEST 4");
    19.  
    20.         UnityWebRequest uwr = UnityWebRequest.Post(url, form);
    21.         yield return uwr.SendWebRequest();
    22.  
    23.         if (uwr.isNetworkError)
    24.         {
    25.             Debug.Log("Error While Sending: " + uwr.error);
    26.         }
    27.         else
    28.         {
    29.             Debug.Log("Received: " + uwr.downloadHandler.text);
    30.         }
    31.     }
    Again this code works fine in the editor, but throws a syntax error when built that specifies you check the Java Console. The build then crashes and no other information is provided.

    I know that XMLHTTPRequest is whatunderlies UnityWebRequest and that it may be possible to build custom plugin to achieve my goal but I am not very familiar with javascript. Here are some of the main posts I have been reviewing (in case they might help someone else):

    https://forum.unity.com/threads/webgl-cors-and-xmlhttprequest-withcredentials.438004/
    https://stackoverflow.com/questions/46003824/sending-http-requests-in-c-sharp-with-unity
    https://forum.unity.com/threads/unity-5-www-and-http-requests.292971/

    Anybody have any ideas?
     
  2. HyruleDefender

    HyruleDefender

    Joined:
    Apr 4, 2017
    Posts:
    17
    I too have these troubles. Most I could find is a 2016 post from a Unity Technician saying unity's WebGL doesn't support the needed items.

    His Post:
    "To get this to work, I think we would need to support .Net Sockets and Dns.Resolve (which currently are not).

    It might be easier to implement a javascript-specific solution for this.
    "

    I always get "SocketException host: 'stmp.gmail.com' cannot be reached"
    This doesnt happen in editor or in pc/mac builds. Only webgl.
     
  3. AlexHell

    AlexHell

    Joined:
    Oct 2, 2014
    Posts:
    167
    What error you take in browser console? (F12 - console) Unitywebrequest (based on xmlhttprequest in js) is work fine for me.
     
  4. HyruleDefender

    HyruleDefender

    Joined:
    Apr 4, 2017
    Posts:
    17
    Hey, I'm not sure if I understand the question correctly, but this is the error I get in each browser when using the developer mode log console
    "SocketException host: 'stmp.gmail.com' cannot be reached"
     
  5. AlexHell

    AlexHell

    Joined:
    Oct 2, 2014
    Posts:
    167
    I'm reply to topic-starter message, in his case I'm not understand the error.

    But in your case, imho, you don't need to send smtp.. and I don't ever know if smtp sent can be done in WebGL.. instead you can send regular HTTP (or https) with UnityWebRequest to correct host, which provides API to you. As I understand you must have API service, which will proxy your HTTP request to correct service. In any case, for security reason, imho, you don't paste your credentials to WebGL game, and better to invoke server code, of any API, or of your (controlled) server, which will invoke smtp-related code.
     
  6. goldv72

    goldv72

    Joined:
    Feb 13, 2020
    Posts:
    6
    sMTp!! not stmp