graphQL-client-unity icon indicating copy to clipboard operation
graphQL-client-unity copied to clipboard

Does not work in WebGL due to threading

Open isurfraz opened this issue 5 years ago • 6 comments

Can a version using co-routines be added?

isurfraz avatar Oct 16 '20 14:10 isurfraz

+1

zmays avatar Dec 02 '20 15:12 zmays

I have been able to make it run on a WebGL build. What I basically did was to rely on UniTask instead of the AsyncAwaitUtil package integrated with the GraphQL client.

I didn't retry all the steps I followed on a fresh project so I may forget something but I'll do my best to be comprehensive:

  1. Install the UniTask package. In my case, I did the install using the Git URL but either way should be fine.
  2. Open the GraphQL client Unity package with Unity.
  3. When prompted to select the files you wish to import, uncheck the AsyncAwaitUtil folder under plugins.
  4. Now that everything is imported, you should see the following compile error in HttpHandler.cs on line 33 and 58:

'UnityWebRequestAsyncOperation' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'UnityWebRequestAsyncOperation' could be found (are you missing a using directive or an assembly reference?)

  1. That's because we didn't import the AsyncAwaitUtil which provides the definition for GetAwaiter. To fix this, import the interface provided by UniTask by adding this line at the top of HttpHandler.cs: using Cysharp.Threading.Tasks; as follows:
Screenshot_2020-12-08_at_10_07_29

Everything should work fine now.

Unfortunately, the steps above imply to update the code of a third-party package (in that case, graphQL-client-unity), which is never a good idea as this will break upon package updates. It would be better if we added this directly in the package itself, but there might be unwanted side effects by relying on UniTask instead of the provided package. Maybe @Gazuntype could give his opinion on this :)

jtiret avatar Dec 08 '20 09:12 jtiret

I guess subscriptions will not work either because websocket-connections in webgl are only possible via a seperate jslib?

cmadh avatar Feb 05 '21 14:02 cmadh

@cmadh that's right, the implementation of WebSocket in this library doesn't work with WebGL. I had to do it myself. In case you want to use WebSocket with WebGL, I advise you to use this library. Even for uses cases without WebGL as a build target, I find its APIs easier to use than the regular System.Net.WebSockets.

jtiret avatar Feb 19 '21 14:02 jtiret

@cmadh that's right, the implementation of WebSocket in this library doesn't work with WebGL. I had to do it myself. In case you want to use WebSocket with WebGL, I advise you to use this library. Even for uses cases without WebGL as a build target, I find its APIs easier to use than the regular System.Net.WebSockets.

@jtiret thank you! Any chance you make your modified fork public? Do you know if the ws-library you recommended is able to handle multiple websocket-instances? I have multiple plugins relying on a websocket-connection and so far I havn't found a solution to use them in parallel because all the alternative webgl-ws-libs only work with a single instance...

cmadh avatar Mar 13 '21 13:03 cmadh

When prompted to select the files you wish to import, uncheck the AsyncAwaitUtil folder under plugins.

@jtiret I'm afraid the plugins folder doesn't even show up when importing this package: https://github.com/gazuntype/graphQL-client-unity/blob/master/UnityPackages/graphQl-client-unity-v2.unitypackage

Or are there different ways of importing?

image

memark avatar Sep 10 '21 05:09 memark