From 92e6511d9156955eb3337f1f38383e88169f5dc0 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Thu, 17 Sep 2026 22:25:28 +0800 Subject: [PATCH] feat(livestreamassist): add a simple example --- LiveStreamAssist/README.md | 2 + .../examples/research-overlay.html | 121 ++++++++++++++++++ LiveStreamAssist/tools/Test-WebSocketApi.ps1 | 64 +++++---- 3 files changed, 163 insertions(+), 24 deletions(-) create mode 100644 LiveStreamAssist/examples/research-overlay.html diff --git a/LiveStreamAssist/README.md b/LiveStreamAssist/README.md index dae0648..384f3e6 100644 --- a/LiveStreamAssist/README.md +++ b/LiveStreamAssist/README.md @@ -26,5 +26,7 @@ Port = 18080 Then connect to `ws://:18080/api/v1`. There is no authentication. Use a LAN bind address or `0.0.0.0` only on trusted networks. Configuration changes require a game restart. +Open [examples/research-overlay.html](https://github.com/soarqin/DSP_Mods/blob/master/LiveStreamAssist/examples/research-overlay.html) in a browser or OBS Browser Source to show the current research ID, level, and hash progress. Default URL is `ws://127.0.0.1:18080/api/v1`; override with `?ws=`. + - [WebSocket API reference](https://github.com/soarqin/DSP_Mods/blob/master/LiveStreamAssist/docs/WebSocketApi.md) - [WebSocket design and transport migration plan](https://github.com/soarqin/DSP_Mods/blob/master/LiveStreamAssist/docs/WebSocketDesign.md) diff --git a/LiveStreamAssist/examples/research-overlay.html b/LiveStreamAssist/examples/research-overlay.html new file mode 100644 index 0000000..4aabbda --- /dev/null +++ b/LiveStreamAssist/examples/research-overlay.html @@ -0,0 +1,121 @@ + + + + + LiveStreamAssist research + + + +
+ +
Current research
+
Connecting…
+
+
+
+
+ + + diff --git a/LiveStreamAssist/tools/Test-WebSocketApi.ps1 b/LiveStreamAssist/tools/Test-WebSocketApi.ps1 index 29b2f58..0a6130a 100644 --- a/LiveStreamAssist/tools/Test-WebSocketApi.ps1 +++ b/LiveStreamAssist/tools/Test-WebSocketApi.ps1 @@ -21,66 +21,82 @@ function New-OpCts { [Threading.CancellationTokenSource]::new([TimeSpan]::FromSeconds($TimeoutSec)) } +function Get-ConnSocket { + param($Conn) + if ($null -eq $Conn) { return $null } + if ($Conn -is [hashtable]) { + if ($Conn.ContainsKey('Ws')) { return $Conn['Ws'] } + return $null + } + if ($Conn.PSObject.Properties.Name -contains 'Ws') { return $Conn.Ws } + return $null +} + function Connect-Api { param([string] $Uri) $ws = [Net.WebSockets.ClientWebSocket]::new() $cts = New-OpCts try { - $ws.ConnectAsync([Uri]$Uri, $cts.Token).GetAwaiter().GetResult() + $null = $ws.ConnectAsync([Uri]$Uri, $cts.Token).GetAwaiter().GetResult() } catch { - $ws.Dispose() + $null = $ws.Dispose() throw } finally { - $cts.Dispose() + $null = $cts.Dispose() } - return @{ Ws = $ws } + [pscustomobject]@{ Ws = $ws } } function Close-Api { param($Conn) - if (-not $Conn) { return } + $ws = Get-ConnSocket $Conn + if ($null -eq $ws) { return } $cts = New-OpCts try { - if ($Conn.Ws.State -eq [Net.WebSockets.WebSocketState]::Open -or - $Conn.Ws.State -eq [Net.WebSockets.WebSocketState]::CloseReceived) { - $Conn.Ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, '', $cts.Token).GetAwaiter().GetResult() + if ($ws.State -eq [Net.WebSockets.WebSocketState]::Open -or + $ws.State -eq [Net.WebSockets.WebSocketState]::CloseReceived) { + $null = $ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, '', $cts.Token).GetAwaiter().GetResult() } } catch { } - finally { $cts.Dispose() } - try { $Conn.Ws.Dispose() } catch { } + finally { $null = $cts.Dispose() } + try { $null = $ws.Dispose() } catch { } } function Send-Text { param($Conn, [string] $Text, [bool] $End = $true) + $ws = Get-ConnSocket $Conn $bytes = $utf8.GetBytes($Text) $seg = [ArraySegment[byte]]::new($bytes) $cts = New-OpCts try { - $Conn.Ws.SendAsync($seg, [Net.WebSockets.WebSocketMessageType]::Text, $End, $cts.Token).GetAwaiter().GetResult() - } finally { $cts.Dispose() } + $null = $ws.SendAsync($seg, [Net.WebSockets.WebSocketMessageType]::Text, $End, $cts.Token).GetAwaiter().GetResult() + } finally { $null = $cts.Dispose() } } function Receive-Message { param($Conn) + $ws = Get-ConnSocket $Conn $buffer = [byte[]]::new(65536) $ms = [IO.MemoryStream]::new() $cts = New-OpCts try { do { $seg = [ArraySegment[byte]]::new($buffer) - $result = $Conn.Ws.ReceiveAsync($seg, $cts.Token).GetAwaiter().GetResult() + $result = $ws.ReceiveAsync($seg, $cts.Token).GetAwaiter().GetResult() if ($result.MessageType -eq [Net.WebSockets.WebSocketMessageType]::Close) { - return @{ Closed = $true; CloseStatus = [int]$Conn.Ws.CloseStatus; Text = $null } + $code = 0 + if ($null -ne $ws.CloseStatus) { $code = [int]$ws.CloseStatus } + return [pscustomobject]@{ Closed = $true; CloseStatus = $code; Text = $null } } if ($result.MessageType -ne [Net.WebSockets.WebSocketMessageType]::Text -or $ms.Length + $result.Count -gt 262144) { throw 'Expected a text response within maxResponseBytes' } - $ms.Write($buffer, 0, $result.Count) + $null = $ms.Write($buffer, 0, $result.Count) } while (-not $result.EndOfMessage) - return @{ Closed = $false; Text = $utf8.GetString($ms.ToArray()) } + return [pscustomobject]@{ Closed = $false; CloseStatus = 0; Text = $utf8.GetString($ms.ToArray()) } } finally { - $ms.Dispose() - $cts.Dispose() + $null = $ms.Dispose() + $null = $cts.Dispose() } } @@ -167,12 +183,12 @@ try { $cts = New-OpCts try { - $conn.Ws.SendAsync( + $null = (Get-ConnSocket $conn).SendAsync( [ArraySegment[byte]]::new([byte[]](1, 2, 3)), [Net.WebSockets.WebSocketMessageType]::Binary, $true, $cts.Token).GetAwaiter().GetResult() - } finally { $cts.Dispose() } + } finally { $null = $cts.Dispose() } $bin = Receive-Message $conn Assert-True ($bin.Closed -and $bin.CloseStatus -eq 1003) "binary rejected $($bin.CloseStatus)" @@ -200,10 +216,10 @@ try { $response = $http.SendAsync($request, [Net.Http.HttpCompletionOption]::ResponseHeadersRead, $cts.Token).GetAwaiter().GetResult() Assert-True ([int]$response.StatusCode -eq 404) 'wrong path HTTP rejection' } finally { - if ($null -ne $response) { $response.Dispose() } - $request.Dispose() - $http.Dispose() - $cts.Dispose() + if ($null -ne $response) { $null = $response.Dispose() } + $null = $request.Dispose() + $null = $http.Dispose() + $null = $cts.Dispose() } $conn = Connect-Api $ServerUri