Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Support network status information #71941

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
<PropertyGroup>
<StrongNameKeyId>Microsoft</StrongNameKeyId>
<IncludePlatformAttributes>true</IncludePlatformAttributes>
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ public partial class NetworkChange
public NetworkChange() { }
[System.Runtime.Versioning.UnsupportedOSPlatform("illumos")]
[System.Runtime.Versioning.UnsupportedOSPlatform("solaris")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static event System.Net.NetworkInformation.NetworkAddressChangedEventHandler? NetworkAddressChanged { add { } remove { } }
[System.Runtime.Versioning.UnsupportedOSPlatform("illumos")]
[System.Runtime.Versioning.UnsupportedOSPlatform("solaris")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris;$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
Expand All @@ -27,7 +27,7 @@
<Compile Include="System\Net\NetworkInformation\MulticastIPAddressInformationCollection.cs" />
<Compile Include="System\Net\NetworkInformation\NetBiosNodeType.cs" />
<Compile Include="System\Net\NetworkInformation\NetEventSource.NetworkInformation.cs" />
<Compile Include="System\Net\NetworkInformation\NetworkAddressChange.cs" Condition="'$(TargetPlatformIdentifier)' != 'illumos' and '$(TargetPlatformIdentifier)' != 'Solaris'" />
<Compile Include="System\Net\NetworkInformation\NetworkAddressChange.cs" Condition="'$(TargetPlatformIdentifier)' != 'illumos' and '$(TargetPlatformIdentifier)' != 'Solaris' and '$(TargetPlatformIdentifier)' != 'Browser'" />
<Compile Include="System\Net\NetworkInformation\NetworkAvailabilityEventArgs.cs" />
<Compile Include="System\Net\NetworkInformation\NetworkChangeDelegates.cs" />
<Compile Include="System\Net\NetworkInformation\NetworkInterface.cs" />
Expand Down Expand Up @@ -159,6 +159,16 @@
<Compile Include="System\Net\NetworkInformation\NetworkInterfacePal.Android.cs" />
<Compile Include="System\Net\NetworkInformation\AndroidNetworkInterface.cs" />
</ItemGroup>
<!-- Android -->
maraf marked this conversation as resolved.
Show resolved Hide resolved
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<Compile Include="System\Net\NetworkInformation\IPGlobalPropertiesPal.Browser.cs" />
<Compile Include="System\Net\NetworkInformation\NetworkInterfacePal.Browser.cs" />
<Compile Include="System\Net\NetworkInformation\NetworkAddressChange.Browser.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\gen\JSImportGenerator\JSImportGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<Reference Include="System.Runtime.InteropServices.JavaScript" />
</ItemGroup>
<!-- OSX -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'OSX' or '$(TargetPlatformIdentifier)' == 'iOS' or '$(TargetPlatformIdentifier)' == 'tvOS' or '$(TargetPlatformIdentifier)' == 'FreeBSD'">
<Compile Include="System\Net\NetworkInformation\IPGlobalPropertiesPal.Bsd.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Net.NetworkInformation
{
internal static class IPGlobalPropertiesPal
{
public static IPGlobalProperties GetIPGlobalProperties() => throw new PlatformNotSupportedException();
maraf marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Win32.SafeHandles;

using System.ComponentModel;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.Versioning;
using System.Threading;

namespace System.Net.NetworkInformation
{
public partial class NetworkChange
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do this like other platform specific partial classes like NetworkAddressChange.Unix.cs?

Copy link
Member Author

@maraf maraf Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what do you mean. Can you please give me a clue?

{
private static event NetworkAvailabilityChangedEventHandler? s_networkAvailabilityChanged;

[UnsupportedOSPlatform("illumos")]
[UnsupportedOSPlatform("solaris")]
public static event NetworkAvailabilityChangedEventHandler? NetworkAvailabilityChanged
{
add
{
if (s_networkAvailabilityChanged == null)
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
BrowserNetworkInterfaceInterop.AddChangeListener(OnNetworkChanged);

s_networkAvailabilityChanged += value;
}
remove
{
s_networkAvailabilityChanged -= value;
maraf marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static void OnNetworkChanged(bool isOnline)
{
if (s_networkAvailabilityChanged != null)
s_networkAvailabilityChanged?.Invoke(null, new NetworkAvailabilityEventArgs(isOnline));
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("illumos")]
[UnsupportedOSPlatform("solaris")]
public static event NetworkAddressChangedEventHandler? NetworkAddressChanged
{
add => throw new PlatformNotSupportedException();
remove => throw new PlatformNotSupportedException();
}

[EditorBrowsableAttribute(EditorBrowsableState.Never)]
[Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.", true)]
public NetworkChange()
{
}

// Introduced for supporting design-time loading of System.Windows.dll
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.", true)]
public static void RegisterNetworkChange(NetworkChange nc) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public partial class NetworkChange
private static readonly AutoResetEvent s_runLoopStartedEvent = new AutoResetEvent(false);
private static readonly AutoResetEvent s_runLoopEndedEvent = new AutoResetEvent(false);

[UnsupportedOSPlatform("browser")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, this shouldn't be needed in platform specific implementation files, since they are included in the build only for the relevant platforms. Same for others.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? If I hadn't have them, the ApiCompat was complaining.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to include the main .cs file also. What was the output from apicompat?

Copy link
Member Author

@maraf maraf Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I just drop this one and try to compile with

.\dotnet.cmd build /p:TargetOS=Browser /p:TargetArchitecture=wasm /p:Configuration=Debug .\src\libraries\System.Net.NetworkInformation\src\

it fails with

....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(52,5): error : Compat issues with assembly System.Net.NetworkInformation: [....\src\libraries\System.Net.NetworkInformation\src\Sy
stem.Net.NetworkInformation.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(52,5): error : CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Net.NetworkInformation.NetworkChange
.NetworkAddressChanged' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("illumos")]' in the implementation. [....\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation
.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(66,5): error : ApiCompat failed for '....\artifacts\bin\System.Net.NetworkInformation\Debug\net7.0-tvos\System.Net.NetworkInformat
ion.dll' [....\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(52,5): error : Compat issues with assembly System.Net.NetworkInformation: [....\src\libraries\System.Net.NetworkInformation\src\Sy
stem.Net.NetworkInformation.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(52,5): error : CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Net.NetworkInformation.NetworkChange
.NetworkAddressChanged' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("illumos")]' in the implementation. [....\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation
.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(66,5): error : ApiCompat failed for '....\artifacts\bin\System.Net.NetworkInformation\Debug\net7.0-ios\System.Net.NetworkInformati
on.dll' [....\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(52,5): error : Compat issues with assembly System.Net.NetworkInformation: [....\src\libraries\System.Net.NetworkInformation\src\Sy
stem.Net.NetworkInformation.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(52,5): error : CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Net.NetworkInformation.NetworkChange
.NetworkAddressChanged' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("illumos")]' in the implementation. [....\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation
.csproj]
....\.nuget\packages\microsoft.dotnet.apicompat\7.0.0-beta.22358.3\build\Microsoft.DotNet.ApiCompat.targets(66,5): error : ApiCompat failed for '....\artifacts\bin\System.Net.NetworkInformation\Debug\net7.0-osx\System.Net.NetworkInformati
on.dll' [....\src\libraries\System.Net.NetworkInformation\src\System.Net.NetworkInformation.csproj]

[UnsupportedOSPlatform("illumos")]
[UnsupportedOSPlatform("solaris")]
public static event NetworkAddressChangedEventHandler? NetworkAddressChanged
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public partial class NetworkChange
private static Timer? s_availabilityTimer;
private static bool s_availabilityHasChanged;

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("illumos")]
[UnsupportedOSPlatform("solaris")]
public static event NetworkAddressChangedEventHandler? NetworkAddressChanged
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public NetworkChange()
{
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("illumos")]
[UnsupportedOSPlatform("solaris")]
public static event NetworkAddressChangedEventHandler? NetworkAddressChanged
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class NetworkChange
}
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("illumos")]
[UnsupportedOSPlatform("solaris")]
public static event NetworkAddressChangedEventHandler? NetworkAddressChanged
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices.JavaScript;

namespace System.Net.NetworkInformation
{
internal static partial class BrowserNetworkInterfaceInterop
{
[JSImport("INTERNAL.network_wasm_online")]
public static partial bool IsOnline();

[JSImport("INTERNAL.network_wasm_add_change_listener")]
public static partial void AddChangeListener([JSMarshalAs<JSType.Function<JSType.Boolean>>] Action<bool> handler);
}

internal static class NetworkInterfacePal
{
public static NetworkInterface[] GetAllNetworkInterfaces() => throw new PlatformNotSupportedException();
public static int IPv6LoopbackInterfaceIndex => throw new PlatformNotSupportedException();
public static int LoopbackInterfaceIndex => throw new PlatformNotSupportedException();
maraf marked this conversation as resolved.
Show resolved Hide resolved

public static bool GetIsNetworkAvailable()
{
return BrowserNetworkInterfaceInterop.IsOnline();
}
maraf marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
maraf marked this conversation as resolved.
Show resolved Hide resolved
public class AddressParsingTests : FileCleanupTestBase
{
[Fact]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class ConnectionsParsingTests : FileCleanupTestBase
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class DnsParsingTests : FileCleanupTestBase
{
[InlineData("NetworkFiles/resolv.conf")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class IPGlobalPropertiesTest
{
private readonly ITestOutputHelper _log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class LoggingTest
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace System.Net.NetworkInformation.Tests
{
// Partial class used for both NetworkAddressChanged and NetworkAvailabilityChanged
// so that the tests for each don't run concurrently
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public partial class NetworkChangedTests
{
private readonly NetworkAddressChangedEventHandler _addressHandler = delegate { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace System.Net.NetworkInformation.Tests
{
// Partial class used for both NetworkAddressChanged and NetworkAvailabilityChanged
// so that the tests for each don't run concurrently
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
maraf marked this conversation as resolved.
Show resolved Hide resolved
public partial class NetworkChangedTests
{
private readonly NetworkAvailabilityChangedEventHandler _availabilityHandler = delegate { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public NetworkInterfaceBasicTest(ITestOutputHelper output)
}

[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public void BasicTest_GetNetworkInterfaces_AtLeastOne()
{
Assert.NotEqual<int>(0, NetworkInterface.GetAllNetworkInterfaces().Length);
Expand Down Expand Up @@ -131,6 +132,7 @@ public void BasicTest_AccessInstanceProperties_NoExceptions_Bsd()

[Fact]
[Trait("IPv4", "true")]
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public void BasicTest_StaticLoopbackIndex_MatchesLoopbackNetworkInterface()
{
Assert.True(Capability.IPv4Support());
Expand All @@ -154,6 +156,7 @@ public void BasicTest_StaticLoopbackIndex_MatchesLoopbackNetworkInterface()

[Fact]
[Trait("IPv4", "true")]
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public void BasicTest_StaticLoopbackIndex_ExceptionIfV4NotSupported()
{
Assert.True(Capability.IPv4Support());
Expand All @@ -163,6 +166,7 @@ public void BasicTest_StaticLoopbackIndex_ExceptionIfV4NotSupported()

[Trait("IPv6", "true")]
[ConditionalFact(typeof(Socket), nameof(Socket.OSSupportsIPv6))]
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public void BasicTest_StaticIPv6LoopbackIndex_MatchesLoopbackNetworkInterface()
{
Assert.True(Capability.IPv6Support());
Expand All @@ -187,6 +191,7 @@ public void BasicTest_StaticIPv6LoopbackIndex_MatchesLoopbackNetworkInterface()

[Trait("IPv6", "true")]
[ConditionalFact(typeof(Socket), nameof(Socket.OSSupportsIPv6))]
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public void BasicTest_StaticIPv6LoopbackIndex_ExceptionIfV6NotSupported()
{
Assert.True(Capability.IPv6Support());
Expand Down Expand Up @@ -277,6 +282,7 @@ public void BasicTest_GetIsNetworkAvailable_Success()
[ActiveIssue("https://github.com/dotnet/runtime/issues/34690", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[SkipOnPlatform(TestPlatforms.OSX | TestPlatforms.FreeBSD, "Expected behavior is different on OSX or FreeBSD")]
[SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
[InlineData(false)]
[InlineData(true)]
public async Task NetworkInterface_LoopbackInterfaceIndex_MatchesReceivedPackets(bool ipv6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class NetworkInterfaceIPv4Statistics
{
private readonly ITestOutputHelper _log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class PhysicalAddressTest
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Net.NetworkInformation.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
public class StatisticsParsingTests : FileCleanupTestBase
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<IgnoreForCI Condition="'$(TargetOS)' == 'Browser'">true</IgnoreForCI>
<DefineConstants>$(DefineConstants);NETWORKINFORMATION_TEST</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<!-- Shared code with src assembly -->
<Compile Include="..\..\src\System\Net\NetworkInformation\StringParsingHelpers.Addresses.cs"
Link="SharedSource\StringParsingHelpers.Addresses.cs" />
Expand Down
5 changes: 5 additions & 0 deletions src/mono/wasm/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { mono_wasm_bind_cs_function, mono_wasm_get_assembly_exports } from "./in
import { mono_wasm_marshal_promise } from "./marshal-to-js";
import { ws_wasm_abort, ws_wasm_close, ws_wasm_create, ws_wasm_open, ws_wasm_receive, ws_wasm_send } from "./web-socket";
import { http_wasm_abort_request, http_wasm_abort_response, http_wasm_create_abort_controler, http_wasm_fetch, http_wasm_fetch_bytes, http_wasm_get_response_bytes, http_wasm_get_response_header_names, http_wasm_get_response_header_values, http_wasm_get_response_length, http_wasm_get_streamed_response_bytes, http_wasm_supports_streaming_response } from "./http";
import { network_wasm_online, network_wasm_add_change_listener } from "./network-information";
import { diagnostics } from "./diagnostics";
import { mono_wasm_cancel_promise } from "./cancelable-promise";
import {
Expand Down Expand Up @@ -485,6 +486,10 @@ const INTERNAL: any = {
http_wasm_get_response_bytes,
http_wasm_get_response_length,
http_wasm_get_streamed_response_bytes,

// NetworkInformation
network_wasm_online,
maraf marked this conversation as resolved.
Show resolved Hide resolved
network_wasm_add_change_listener
};

// this represents visibility in the javascript
Expand Down
11 changes: 11 additions & 0 deletions src/mono/wasm/runtime/network-information.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

export function network_wasm_online(): boolean {
return globalThis.navigator && globalThis.navigator.onLine;
}

export function network_wasm_add_change_listener(listener: (onLine: boolean) => void): void {
window.addEventListener("offline", () => listener(network_wasm_online()));
window.addEventListener("online", () => listener(network_wasm_online()));
}