|
| 1 | +using System.Net; |
| 2 | +using Foundation; |
| 3 | +using Shiny.Net.HttpServer; |
| 4 | +using UIKit; |
| 5 | + |
| 6 | +namespace Sample.TvOS; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// The whole sample: a server on the LAN that follows the app to the foreground. |
| 10 | +/// <para> |
| 11 | +/// tvOS suspends a backgrounded app exactly as iOS does, and there is no background mode that |
| 12 | +/// legitimately holds a listener open on either. So the useful behaviour is not "keep serving" - |
| 13 | +/// that is not on offer - it is to be serving again by the time anyone is looking at the app, and |
| 14 | +/// to make the fact that it stopped visible rather than leaving a listener that reports itself |
| 15 | +/// running with nothing behind it. |
| 16 | +/// </para> |
| 17 | +/// <para> |
| 18 | +/// The lifecycle is wired to UIKit by hand here. <c>Shiny.Net.HttpServer.Mobile</c> does exactly |
| 19 | +/// this and rather more - a bounded retry for the bind a half-woken network refuses, a rebind when |
| 20 | +/// the device changes network - but it does not target tvOS yet, because Shiny.Core does not. When |
| 21 | +/// it does, the two overrides below become <c>AddHttpServerLifecycle()</c> and this file gets |
| 22 | +/// shorter. The core server needed no changes for any of this: it is plain <c>net10.0</c>, and a |
| 23 | +/// tvOS app references it the way anything else does. |
| 24 | +/// </para> |
| 25 | +/// </summary> |
| 26 | +[Register(nameof(AppDelegate))] |
| 27 | +public class AppDelegate : UIApplicationDelegate |
| 28 | +{ |
| 29 | + HttpServer? server; |
| 30 | + ServerViewController? controller; |
| 31 | + |
| 32 | + public override UIWindow? Window { get; set; } |
| 33 | + |
| 34 | + public override bool FinishedLaunching(UIApplication application, NSDictionary? launchOptions) |
| 35 | + { |
| 36 | + this.controller = new ServerViewController(); |
| 37 | + |
| 38 | + // The window-and-delegate lifecycle rather than UIWindowScene. It is obsoleted from |
| 39 | + // tvOS 26 and still works, and the scene-based equivalent is a manifest entry, a scene |
| 40 | + // delegate and a configuration dictionary - all of it ceremony that would bury the six |
| 41 | + // lines this sample is actually about. A real app should adopt scenes. |
| 42 | +#pragma warning disable CA1422 |
| 43 | + this.Window = new UIWindow(UIScreen.MainScreen.Bounds) |
| 44 | + { |
| 45 | + RootViewController = this.controller |
| 46 | + }; |
| 47 | +#pragma warning restore CA1422 |
| 48 | + this.Window.MakeKeyAndVisible(); |
| 49 | + |
| 50 | + this.server = BuildServer(); |
| 51 | + |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + // Not FinishedLaunching: a cold start goes through here too, so starting in one place covers |
| 56 | + // both the first foreground and every one after it. |
| 57 | + public override void WillEnterForeground(UIApplication application) => _ = this.StartAsync(); |
| 58 | + |
| 59 | + public override void OnActivated(UIApplication application) |
| 60 | + { |
| 61 | + // WillEnterForeground is not raised on the very first activation of a cold start. |
| 62 | + if (this.server is { IsRunning: false }) |
| 63 | + _ = this.StartAsync(); |
| 64 | + } |
| 65 | + |
| 66 | + public override void DidEnterBackground(UIApplication application) => _ = this.StopAsync(); |
| 67 | + |
| 68 | + static HttpServer BuildServer() |
| 69 | + { |
| 70 | + var builder = HttpServer.CreateBuilder(); |
| 71 | + |
| 72 | + builder.Configure(o => |
| 73 | + { |
| 74 | + // Any, not Loopback: the point is to be reachable from the other devices on the |
| 75 | + // network. A TV has no browser to test a loopback listener with anyway. |
| 76 | + o.Address = IPAddress.Any; |
| 77 | + o.Port = 8080; |
| 78 | + }); |
| 79 | + |
| 80 | + var server = builder.Build(); |
| 81 | + |
| 82 | + server.MapGet("/", _ => Results.Text(StatusPage(), "text/html; charset=utf-8")); |
| 83 | + server.MapGet("/ping", _ => Results.Text("pong")); |
| 84 | + |
| 85 | + return server; |
| 86 | + } |
| 87 | + |
| 88 | + async Task StartAsync() |
| 89 | + { |
| 90 | + if (this.server is not { IsRunning: false } target) |
| 91 | + return; |
| 92 | + |
| 93 | + try |
| 94 | + { |
| 95 | + await target.StartAsync().ConfigureAwait(false); |
| 96 | + this.Report(); |
| 97 | + } |
| 98 | + catch (Exception ex) |
| 99 | + { |
| 100 | + // A bind refused because the network has not finished waking is the common one, and it |
| 101 | + // is why the Mobile package retries. Shown rather than swallowed: a sample that fails |
| 102 | + // silently teaches the failure mode this repo keeps trying to eliminate. |
| 103 | + this.Report(ex); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + async Task StopAsync() |
| 108 | + { |
| 109 | + if (this.server is not { IsRunning: true } target) |
| 110 | + return; |
| 111 | + |
| 112 | + try |
| 113 | + { |
| 114 | + await target.StopAsync().ConfigureAwait(false); |
| 115 | + } |
| 116 | + catch (Exception) |
| 117 | + { |
| 118 | + // Going to the background regardless. |
| 119 | + } |
| 120 | + |
| 121 | + this.Report(); |
| 122 | + } |
| 123 | + |
| 124 | + void Report(Exception? error = null) |
| 125 | + { |
| 126 | + var running = this.server?.IsRunning ?? false; |
| 127 | + var addresses = LocalAddresses.Current().Select(a => $"http://{a}:8080").ToArray(); |
| 128 | + |
| 129 | + // UIKit is main-thread only and both transitions above complete off it. |
| 130 | + UIApplication.SharedApplication.InvokeOnMainThread( |
| 131 | + () => this.controller?.Update(running, addresses, error) |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + static string StatusPage() |
| 136 | + => """ |
| 137 | + <!doctype html> |
| 138 | + <meta charset="utf-8"> |
| 139 | + <meta name="viewport" content="width=device-width,initial-scale=1"> |
| 140 | + <title>Shiny.Net.HttpServer on tvOS</title> |
| 141 | + <style> |
| 142 | + body { font: 16px/1.6 system-ui, sans-serif; max-width: 34rem; margin: 12vh auto; padding: 0 1.5rem; } |
| 143 | + code { background: #f4f4f5; padding: .15em .4em; border-radius: .25rem; } |
| 144 | + </style> |
| 145 | + <h1>Served from an Apple TV</h1> |
| 146 | + <p> |
| 147 | + This page came from <code>Shiny.Net.HttpServer</code> running inside a tvOS app, on a |
| 148 | + platform where ASP.NET Core cannot go. |
| 149 | + </p> |
| 150 | + <p>The server stops when the app is backgrounded and starts again on resume.</p> |
| 151 | + <p><a href="/ping">/ping</a></p> |
| 152 | + """; |
| 153 | +} |
0 commit comments