Skip to content

Commit

Permalink
feat: Add support for full uri in RC endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Nov 28, 2021
1 parent a083153 commit c5849cc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
74 changes: 45 additions & 29 deletions src/Uno.UI.RemoteControl/RemoteControlClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -19,12 +21,12 @@ namespace Uno.UI.RemoteControl
{
public class RemoteControlClient : IRemoteControlClient
{
public static RemoteControlClient Instance { get; private set; }
public static RemoteControlClient? Instance { get; private set; }

public Type AppType { get; }

private readonly (string endpoint, int port)[] _serverAddresses;
private WebSocket _webSocket;
private readonly (string endpoint, int port)[]? _serverAddresses;
private WebSocket? _webSocket;
private Dictionary<string, IRemoteControlProcessor> _processors = new Dictionary<string, IRemoteControlProcessor>();

private RemoteControlClient(Type appType)
Expand All @@ -37,7 +39,7 @@ private RemoteControlClient(Type appType)
{
foreach (var endpoint in endpoints)
{
if (endpoint.Port == 0)
if (endpoint.Port == 0 && !Uri.TryCreate(endpoint.Endpoint, UriKind.Absolute, out _))
{
this.Log().LogError($"Failed to get remote control server port from the IDE for endpoint {endpoint.Endpoint}.");
}
Expand Down Expand Up @@ -71,47 +73,61 @@ private async Task StartConnection()
{
try
{
async Task<(string endPoint, int port, WebSocket socket)> Connect(string endpoint, int port, CancellationToken ct)
async Task<(Uri endPoint, WebSocket socket)> Connect(string endpoint, int port, CancellationToken ct)
{
#if __WASM__
var s = new Uno.Wasm.WebSockets.WasmWebSocket();
#else
var s = new ClientWebSocket();
#endif

if(port == 443)
Uri BuildServerUri()
{
#if __WASM__
if (endpoint.EndsWith("gitpod.io"))
if (Uri.TryCreate(endpoint, UriKind.Absolute, out var fullUri))
{
var wsScheme = fullUri.Scheme switch
{
"http" => "ws",
"https" => "ws",
_ => throw new InvalidOperationException($"Unsupported remote host scheme ({fullUri})"),
};

return new Uri($"{wsScheme}://{fullUri.Authority}/rc");
}
else if (port == 443)
{
var originParts = endpoint.Split('-');
#if __WASM__
if (endpoint.EndsWith("gitpod.io"))
{
var originParts = endpoint.Split('-');

var currentHost = Foundation.WebAssemblyRuntime.InvokeJS("window.location.hostname");
var targetParts = currentHost.Split('-');
var currentHost = Foundation.WebAssemblyRuntime.InvokeJS("window.location.hostname");
var targetParts = currentHost.Split('-');

endpoint = originParts[0] + '-' + currentHost.Substring(targetParts[0].Length + 1);
}
endpoint = originParts[0] + '-' + currentHost.Substring(targetParts[0].Length + 1);
}
#endif

await s.ConnectAsync(new Uri($"wss://{endpoint}/rc"), ct);
return new Uri($"wss://{endpoint}/rc");
}
else
{
return new Uri($"ws://{endpoint}:{port}/rc");
}
}
else

var serverUri = BuildServerUri();

if (this.Log().IsEnabled(LogLevel.Trace))
{
await s.ConnectAsync(new Uri($"ws://{endpoint}:{port}/rc"), ct);
this.Log().LogTrace($"Connecting to [{serverUri}]");
}

return (endpoint, port, s);
await s.ConnectAsync(serverUri, ct);

return (serverUri, s);
}

var connections = _serverAddresses
.Where(adr => adr.port != 0)
.Where(adr => adr.port != 0 || Uri.TryCreate(adr.endpoint, UriKind.Absolute, out _))
.Select(s =>
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Connecting to {s}...");
}
var cts = new CancellationTokenSource();
var task = Connect(s.endpoint, s.port, cts.Token);
Expand Down Expand Up @@ -146,11 +162,11 @@ private async Task StartConnection()
return;
}

var connected = ((Task<(string endPoint, int port, WebSocket socket)>)completed).Result;
var connected = ((Task<(Uri endPoint, WebSocket socket)>)completed).Result;

if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Connected to {connected.endPoint}:{connected.port}");
this.Log().LogDebug($"Connected to {connected.endPoint}");
}

_webSocket = connected.socket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
<Message Text="&lt;IntermediateOutputPath&gt;$(MSBuildProjectDirectory)/$(IntermediateOutputPath)&lt;/IntermediateOutputPath&gt;" Importance="High" />
</Target>

<Target Name="InjectRemoteControlPort" BeforeTargets="BeforeBuild" Condition="exists('$(IntermediateOutputPath)\RemoteControlPort.config')">
<Target Name="InjectRemoteControlHost" BeforeTargets="BeforeBuild" Condition="exists('$(IntermediateOutputPath)\RemoteControlHost.config')">

<ItemGroup>
<WasmShellMonoEnvironment Include="DOTNET_MODIFIABLE_ASSEMBLIES" Value="debug" />
</ItemGroup>

<ReadLinesFromFile File="$(IntermediateOutputPath)\RemoteControlPort.config" >
<Output TaskParameter="Lines" ItemName="_RemoteControlPortContent"/>
<ReadLinesFromFile File="$(IntermediateOutputPath)\RemoteControlHost.config" >
<Output TaskParameter="Lines" ItemName="_RemoteControlHostContent"/>
</ReadLinesFromFile>

<CreateProperty
Value="@(_RemoteControlPortContent)">
Value="@(_RemoteControlHostContent)">
<Output
TaskParameter="Value"
PropertyName="UnoRemoteControlPort" />
PropertyName="UnoRemoteControlHost" />
</CreateProperty>

</Target>
Expand Down

0 comments on commit c5849cc

Please sign in to comment.