Skip to content

Commit

Permalink
Import TransportLayer logic from C# SDK, add WebGL Threading Patcher,…
Browse files Browse the repository at this point in the history
… implement Unity-side IHttpClientService
  • Loading branch information
jakub-grzesiowski committed Oct 15, 2024
1 parent 4854227 commit 753d2ea
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 8 deletions.
Binary file modified PubNubUnity/Assets/PubNub/Runtime/Plugins/PubnubApiUnity.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public Pubnub Initialize(string userId) {
pnConfiguration.UserId = userId;
pubnub = new Pubnub(pnConfiguration);
pubnub.SetJsonPluggableLibrary(new NewtonsoftJsonUnity(pnConfiguration, ((PNConfiguration)pnConfiguration).PubnubLog));
pubnub.SetTransportLayer(new UnityHttpClientService());
pubnub.AddListener(listener);
return pubnub;

Expand Down
194 changes: 194 additions & 0 deletions PubNubUnity/Assets/PubNub/Runtime/Util/UnityHttpClientService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine.Networking;

namespace PubnubApi.Unity {

public class UnityHttpClientService : IHttpClientService {
private TransportResponse UnityRequestToResponse(UnityWebRequest request) {
return new TransportResponse() {
StatusCode = (int)request.responseCode,
Content = request.downloadHandler?.data,
RequestUrl = request.url,
Error = new Exception(request.error),
Headers = request.GetResponseHeaders()
.ToDictionary(
x => x.Key,
y => (IEnumerable<string>)new[] { y.Value })
};
}

private void PrepareUnityRequest(UnityWebRequest request, TransportRequest transportRequest) {
if (transportRequest.Timeout.HasValue) {
request.timeout = (int)transportRequest.Timeout.Value.TotalSeconds;
}

if (transportRequest.Headers.Keys.Count > 0) {
foreach (var kvp in transportRequest.Headers) {
request.SetRequestHeader(kvp.Key, kvp.Value);
}
}
}

public async Task<TransportResponse> DeleteRequest(TransportRequest transportRequest) {
TransportResponse response;
try {
var deleteRequest = UnityWebRequest.Delete(transportRequest.RequestUrl);
PrepareUnityRequest(deleteRequest, transportRequest);
var taskCompletionSource = new TaskCompletionSource<TransportResponse>();
deleteRequest.SendWebRequest().completed += _ => {
//will raise an exception if cancellation token is triggered
//but shouldn't because we also Abort (probably?)
taskCompletionSource.SetResult(UnityRequestToResponse(deleteRequest));
};
await using (transportRequest.CancellationToken.Register(() => {
deleteRequest.Abort();
taskCompletionSource.TrySetCanceled();
})) {
response = await taskCompletionSource.Task;
}
} catch (Exception ex) {
response = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = ex
};
}

return response;
}

public async Task<TransportResponse> GetRequest(TransportRequest transportRequest) {
TransportResponse response;
try {
var getRequest = UnityWebRequest.Get(transportRequest.RequestUrl);
PrepareUnityRequest(getRequest, transportRequest);
var taskCompletionSource = new TaskCompletionSource<TransportResponse>();
getRequest.SendWebRequest().completed += _ => {
//will raise an exception if cancellation token is triggered
//but shouldn't because we also Abort (probably?)
taskCompletionSource.SetResult(UnityRequestToResponse(getRequest));
};
await using (transportRequest.CancellationToken.Register(() => {
getRequest.Abort();
taskCompletionSource.TrySetCanceled();
})) {
response = await taskCompletionSource.Task;
}
} catch (Exception ex) {
response = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = ex
};
}

return response;
}

public async Task<TransportResponse> PostRequest(TransportRequest transportRequest) {
TransportResponse response;
try {
var formData = new List<IMultipartFormSection>();
if (!string.IsNullOrEmpty(transportRequest.BodyContentString)) {
formData.Add(new MultipartFormDataSection(transportRequest.BodyContentString));
} else if (transportRequest.BodyContentBytes != null) {
formData.Add(new MultipartFormDataSection(transportRequest.BodyContentBytes));
}

var postRequest = UnityWebRequest.Post(transportRequest.RequestUrl, formData);
PrepareUnityRequest(postRequest, transportRequest);
var taskCompletionSource = new TaskCompletionSource<TransportResponse>();
postRequest.SendWebRequest().completed += _ => {
//will raise an exception if cancellation token is triggered
//but shouldn't because we also Abort (probably?)
taskCompletionSource.SetResult(UnityRequestToResponse(postRequest));
};
await using (transportRequest.CancellationToken.Register(() => {
postRequest.Abort();
taskCompletionSource.TrySetCanceled();
})) {
response = await taskCompletionSource.Task;
}
} catch (Exception ex) {
response = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = ex
};
}

return response;
}

public async Task<TransportResponse> PatchRequest(TransportRequest transportRequest) {
TransportResponse response;
try {
UnityWebRequest patchRequest;
if (!string.IsNullOrEmpty(transportRequest.BodyContentString)) {
patchRequest = UnityWebRequest.Put(transportRequest.RequestUrl, transportRequest.BodyContentString);
} else if (transportRequest.BodyContentBytes != null) {
patchRequest = UnityWebRequest.Put(transportRequest.RequestUrl, transportRequest.BodyContentBytes);
} else {
throw new ArgumentException("PATCH Transport Request has no body!");
}
patchRequest.method = "PATCH";

PrepareUnityRequest(patchRequest, transportRequest);
var taskCompletionSource = new TaskCompletionSource<TransportResponse>();
patchRequest.SendWebRequest().completed += _ => {
//will raise an exception if cancellation token is triggered
//but shouldn't because we also Abort (probably?)
taskCompletionSource.SetResult(UnityRequestToResponse(patchRequest));
};
await using (transportRequest.CancellationToken.Register(() => {
patchRequest.Abort();
taskCompletionSource.TrySetCanceled();
})) {
response = await taskCompletionSource.Task;
}
} catch (Exception ex) {
response = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = ex
};
}

return response;
}

public async Task<TransportResponse> PutRequest(TransportRequest transportRequest) {
TransportResponse response;
try {
UnityWebRequest putRequest;
if (!string.IsNullOrEmpty(transportRequest.BodyContentString)) {
putRequest = UnityWebRequest.Put(transportRequest.RequestUrl, transportRequest.BodyContentString);
} else if (transportRequest.BodyContentBytes != null) {
putRequest = UnityWebRequest.Put(transportRequest.RequestUrl, transportRequest.BodyContentBytes);
} else {
throw new ArgumentException("PUT Transport Request has no body!");
}

PrepareUnityRequest(putRequest, transportRequest);
var taskCompletionSource = new TaskCompletionSource<TransportResponse>();
putRequest.SendWebRequest().completed += _ => {
//will raise an exception if cancellation token is triggered
//but shouldn't because we also Abort (probably?)
taskCompletionSource.SetResult(UnityRequestToResponse(putRequest));
};
await using (transportRequest.CancellationToken.Register(() => {
putRequest.Abort();
taskCompletionSource.TrySetCanceled();
})) {
response = await taskCompletionSource.Task;
}
} catch (Exception ex) {
response = new TransportResponse() {
RequestUrl = transportRequest.RequestUrl,
Error = ex
};
}

return response;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PubNubUnity/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"com.tools.webglthreadingpatcher": "https://github.com/VolodymyrBS/WebGLThreadingPatcher.git",
"com.unity.2d.sprite": "1.0.0",
"com.unity.2d.tilemap": "1.0.0",
"com.unity.ads": "3.7.5",
Expand Down
40 changes: 32 additions & 8 deletions PubNubUnity/Packages/packages-lock.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"dependencies": {
"com.tools.webglthreadingpatcher": {
"version": "https://github.com/VolodymyrBS/WebGLThreadingPatcher.git",
"depth": 0,
"source": "git",
"dependencies": {
"nuget.mono-cecil": "0.1.6-preview"
},
"hash": "c2f3628a028373afc8c66e443f7ce21bcb613a47"
},
"com.unity.2d.sprite": {
"version": "1.0.0",
"depth": 0,
Expand Down Expand Up @@ -78,6 +87,13 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.mono-cecil": {
"version": "1.10.1",
"depth": 2,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.newtonsoft-json": {
"version": "3.0.2",
"depth": 0,
Expand All @@ -91,12 +107,12 @@
"source": "registry",
"dependencies": {
"com.unity.ugui": "1.0.0",
"com.unity.modules.unityanalytics": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
"com.unity.services.core": "1.3.1",
"com.unity.services.analytics": "4.0.1"
"com.unity.modules.androidjni": "1.0.0",
"com.unity.services.analytics": "4.0.1",
"com.unity.modules.jsonserialize": "1.0.0",
"com.unity.modules.unityanalytics": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0"
},
"url": "https://packages.unity.com"
},
Expand All @@ -115,9 +131,9 @@
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.modules.unitywebrequest": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
"com.unity.nuget.newtonsoft-json": "3.0.2",
"com.unity.modules.androidjni": "1.0.0"
"com.unity.modules.unitywebrequest": "1.0.0"
},
"url": "https://packages.unity.com"
},
Expand Down Expand Up @@ -162,9 +178,9 @@
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.director": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.particlesystem": "1.0.0"
},
"url": "https://packages.unity.com"
Expand Down Expand Up @@ -208,6 +224,14 @@
},
"url": "https://packages.unity.com"
},
"nuget.mono-cecil": {
"version": "1.0.0",
"depth": 1,
"source": "builtin",
"dependencies": {
"com.unity.nuget.mono-cecil": "1.10.1"
}
},
"com.unity.modules.ai": {
"version": "1.0.0",
"depth": 0,
Expand Down

0 comments on commit 753d2ea

Please sign in to comment.