Skip to content

Commit b4a302b

Browse files
committed
fix: send unity realtime subscriptions
1 parent edfe320 commit b4a302b

15 files changed

Lines changed: 353 additions & 177 deletions

File tree

mock-server/app/http.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
use Utopia\Validator\Nullable;
2626
use Utopia\Validator\WhiteList;
2727
use Swoole\Process;
28-
use Swoole\Http\Server;
2928
use Utopia\MockServer\Utopia\Model\Player;
3029
use Utopia\MockServer\Utopia\Validator\Player as PlayerValidator;
30+
use Swoole\WebSocket\Frame;
31+
use Swoole\WebSocket\Server as WebSocketServer;
3132

3233
const APP_AUTH_TYPE_SESSION = 'Session';
3334
const APP_AUTH_TYPE_JWT = 'JWT';
@@ -39,7 +40,7 @@
3940
const APP_PLATFORM_CONSOLE = 'console';
4041
const APP_STORAGE_CACHE = '/storage/cache';
4142

42-
$http = new Server(
43+
$http = new WebSocketServer(
4344
host: '0.0.0.0',
4445
port: App::getEnv('PORT', 80),
4546
mode: SWOOLE_PROCESS
@@ -862,7 +863,7 @@ function ($utopia, $error, $request, $response) {
862863
['utopia', 'error', 'request', 'response']
863864
);
864865

865-
$http->on(Constant::EVENT_START, function (Server $http) use ($payloadSize) {
866+
$http->on(Constant::EVENT_START, function (WebSocketServer $http) use ($payloadSize) {
866867
Console::success('Server started successfully (max payload is ' . number_format($payloadSize) . ' bytes)');
867868
Console::info("Master pid {$http->master_pid}, manager pid {$http->manager_pid}");
868869

@@ -885,4 +886,63 @@ function () use ($http) {
885886
$app->run($request, $response);
886887
});
887888

889+
$http->on('open', function (WebSocketServer $http, SwooleRequest $request) {
890+
$http->push($request->fd, \json_encode([
891+
'type' => 'connected',
892+
'data' => [
893+
'user' => null,
894+
'subscriptions' => (object) [],
895+
],
896+
]));
897+
});
898+
899+
$http->on('message', function (WebSocketServer $http, Frame $frame) {
900+
$message = \json_decode($frame->data, true);
901+
if (!\is_array($message)) {
902+
return;
903+
}
904+
905+
if (($message['type'] ?? '') === 'ping') {
906+
$http->push($frame->fd, \json_encode(['type' => 'pong']));
907+
return;
908+
}
909+
910+
if (($message['type'] ?? '') !== 'subscribe') {
911+
return;
912+
}
913+
914+
foreach (($message['data'] ?? []) as $subscription) {
915+
$subscriptionId = $subscription['subscriptionId'] ?? '';
916+
if (empty($subscriptionId)) {
917+
continue;
918+
}
919+
920+
$queries = $subscription['queries'] ?? [];
921+
$shouldSendEvent = true;
922+
foreach ($queries as $query) {
923+
if (\is_string($query) && \str_contains($query, '"failed"')) {
924+
$shouldSendEvent = false;
925+
break;
926+
}
927+
}
928+
929+
if (!$shouldSendEvent) {
930+
continue;
931+
}
932+
933+
$http->push($frame->fd, \json_encode([
934+
'type' => 'event',
935+
'data' => [
936+
'events' => ['tests.*'],
937+
'channels' => ['tests'],
938+
'subscriptions' => [$subscriptionId],
939+
'timestamp' => \date(DATE_ATOM),
940+
'payload' => [
941+
'response' => 'WS:/v1/realtime:passed',
942+
],
943+
],
944+
]));
945+
}
946+
});
947+
888948
$http->start();

src/SDK/Language/Unity.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getFiles(): array
7979
'destination' => 'Assets/Runtime/{{ spec.title | caseUcfirst }}.asmdef',
8080
'template' => 'unity/Assets/Runtime/Appwrite.asmdef.twig',
8181
],
82-
// Appwrite
82+
// Runtime
8383
[
8484
'scope' => 'default',
8585
'destination' => 'Assets/Runtime/{{ spec.title | caseUcfirst }}Config.cs',
@@ -105,7 +105,7 @@ public function getFiles(): array
105105
'destination' => 'Assets/Runtime/Utilities/{{ spec.title | caseUcfirst }}Utilities.cs',
106106
'template' => 'unity/Assets/Runtime/Utilities/AppwriteUtilities.cs.twig',
107107
],
108-
// Appwrite.Core
108+
// Runtime core
109109
[
110110
'scope' => 'copy',
111111
'destination' => 'Assets/Runtime/Core/csc.rsp',
@@ -243,7 +243,7 @@ public function getFiles(): array
243243
'template' => 'unity/Assets/Runtime/Core/Plugins/System.Text.Encodings.Web.dll',
244244
],
245245
[
246-
'scope' => 'copy',
246+
'scope' => 'default',
247247
'destination' => 'Assets/Runtime/Core/Plugins/Android/AndroidManifest.xml',
248248
'template' => 'unity/Assets/Runtime/Core/Plugins/Android/AndroidManifest.xml',
249249
],
@@ -257,7 +257,12 @@ public function getFiles(): array
257257
'destination' => 'Assets/Runtime/Core/Plugins/System.Text.Json.dll',
258258
'template' => 'unity/Assets/Runtime/Core/Plugins/System.Text.Json.dll',
259259
],
260-
// Appwrite.Editor
260+
[
261+
'scope' => 'copy',
262+
'destination' => 'Assets/Runtime/Core/Plugins/THIRD_PARTY_NOTICES.md',
263+
'template' => 'unity/Assets/Runtime/Core/Plugins/THIRD_PARTY_NOTICES.md',
264+
],
265+
// Editor
261266
[
262267
'scope' => 'default',
263268
'destination' => 'Assets/Editor/{{ spec.title | caseUcfirst }}.Editor.asmdef',
@@ -362,7 +367,7 @@ public function getFiles(): array
362367
'template' => 'unity/ProjectSettings/PresetManager.asset',
363368
],
364369
[
365-
'scope' => 'copy',
370+
'scope' => 'default',
366371
'destination' => 'ProjectSettings/ProjectSettings.asset',
367372
'template' => 'unity/ProjectSettings/ProjectSettings.asset',
368373
],
@@ -408,6 +413,16 @@ public function getFiles(): array
408413
],
409414
];
410415

416+
foreach ($files as &$file) {
417+
if (
418+
str_starts_with($file['destination'], 'Packages/')
419+
|| str_starts_with($file['destination'], 'ProjectSettings/')
420+
) {
421+
$file['test'] = true;
422+
}
423+
}
424+
unset($file);
425+
411426
// Filter out problematic files in test mode
412427
// Check if we're in test mode by looking for a global variable
413428
if (isset($GLOBALS['UNITY_TEST_MODE']) && $GLOBALS['UNITY_TEST_MODE'] === true) {

src/SDK/SDK.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,10 @@ public function generate(string $target): void
10291029
];
10301030

10311031
foreach ($this->language->getFiles() as $file) {
1032+
if (($file['test'] ?? false) && $this->getParam('test') !== 'true') {
1033+
continue;
1034+
}
1035+
10321036
if ($file['scope'] != 'copy') {
10331037
$template = $this->twig->load($file['template']); /* @var $template TemplateWrapper */
10341038
}

templates/unity/Assets/Editor/AppwriteSetupWindow.cs.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ namespace {{ spec.title | caseUcfirst }}.Editor
204204
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
205205
EditorGUILayout.BeginHorizontal();
206206
var buttonStyle = new GUIStyle(GUI.skin.button) { padding = new RectOffset(15, 15, 8, 8), margin = new RectOffset(5, 5, 5, 5), fontSize = 11 };
207-
if (GUILayout.Button(new GUIContent(" 📖 Documentation", "Open {{ spec.title | caseUcfirst }} documentation"), buttonStyle)) { Application.OpenURL("https://appwrite.io/docs"); }
207+
if (GUILayout.Button(new GUIContent(" 📖 Documentation", "Open {{ spec.title | caseUcfirst }} documentation"), buttonStyle)) { Application.OpenURL("{{ sdk.url }}"); }
208208
if (GUILayout.Button(new GUIContent(" 💬 Discord Community", "Join our Discord community"), buttonStyle)) { Application.OpenURL("https://discord.gg/dJ9xrMr9hq"); }
209209
EditorGUILayout.EndHorizontal();
210210
EditorGUILayout.EndVertical();
@@ -253,4 +253,4 @@ namespace {{ spec.title | caseUcfirst }}.Editor
253253
}
254254
}
255255
}
256-
}
256+
}

templates/unity/Assets/Runtime/Core/Client.cs.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ namespace {{ spec.title | caseUcfirst }}
274274

275275
foreach (var parameter in parameters)
276276
{
277-
if (parameter.Key == "file" && parameter.Value is InputFile inputFile)
277+
if (parameter.Value is InputFile inputFile)
278278
{
279279
byte[] fileData = {};
280280
switch (inputFile.SourceType)
@@ -570,6 +570,8 @@ namespace {{ spec.title | caseUcfirst }}
570570
var stream = input.Data as Stream;
571571
if (stream == null)
572572
throw new InvalidOperationException("Stream data is null");
573+
if (!stream.CanSeek)
574+
throw new ArgumentException("Stream must be seekable for chunked uploads.", paramName);
573575
size = stream.Length;
574576
break;
575577
case "bytes":

templates/unity/Assets/Runtime/Core/Plugins/Android/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
<action android:name="android.intent.action.MAIN" />
1111
<category android:name="android.intent.category.LAUNCHER" />
1212
</intent-filter>
13-
<!-- Appwrite OAuth deeplink -->
13+
<!-- {{ spec.title | caseUcfirst }} OAuth deeplink -->
1414
<intent-filter>
1515
<action android:name="android.intent.action.VIEW" />
1616
<category android:name="android.intent.category.DEFAULT" />
1717
<category android:name="android.intent.category.BROWSABLE" />
18-
<data android:scheme="appwrite-callback-<YOUR_PROJECT_ID>" />
18+
<data android:scheme="{{ spec.title | caseLower }}-callback-<YOUR_PROJECT_ID>" />
1919
</intent-filter>
2020
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
2121
</activity>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Third-party runtime plugins
2+
3+
The DLLs in this directory are copied from NuGet packages published by Microsoft
4+
and are included so the Unity SDK can use `System.Text.Json` on Unity 2021.
5+
6+
To update these files, download the same package names from NuGet, copy the
7+
`lib/netstandard2.0/*.dll` files into this directory, and update the table below
8+
with the new package versions and SHA-256 checksums.
9+
10+
| File | NuGet package | Version | SHA-256 |
11+
| --- | --- | --- | --- |
12+
| `Microsoft.Bcl.AsyncInterfaces.dll` | `Microsoft.Bcl.AsyncInterfaces` | `9.0.6` | `eb507b48362b3b0599ad7f236da1bfba9da386c37301493dbfb725d5116e5842` |
13+
| `System.IO.Pipelines.dll` | `System.IO.Pipelines` | `9.0.6` | `ca8ecbb502543c635fd53bfb41c6b6e68cd2ecad5e4464f2492f78edc3192d3b` |
14+
| `System.Runtime.CompilerServices.Unsafe.dll` | `System.Runtime.CompilerServices.Unsafe` | `6.0.0` | `01748200f2400c742aa689f1f5101bd6298efdfd92c00c18f4fa473847235ba9` |
15+
| `System.Text.Encodings.Web.dll` | `System.Text.Encodings.Web` | `9.0.6` | `7089712df9f3f07862317652cbcca613c15fee759209f9b9d0c624dc81538829` |
16+
| `System.Text.Json.dll` | `System.Text.Json` | `9.0.6` | `bf1f8a674d9bb26bc7af4cefc2936bf4bda637dc9d3b71d6ca77c0b6a254fa83` |
17+
18+
These Microsoft packages are licensed under the MIT license. See each package's
19+
NuGet metadata for full license and repository details.

templates/unity/Assets/Runtime/Core/WebAuthComponent.cs.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System;
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
5-
using Appwrite.Extensions;
5+
using {{ spec.title | caseUcfirst }}.Extensions;
66
using Cysharp.Threading.Tasks;
77
using UnityEngine;
88

@@ -99,4 +99,4 @@ namespace {{ spec.title | caseUcfirst }}
9999
#endif
100100
}
101101
}
102-
#endif
102+
#endif

0 commit comments

Comments
 (0)