Files
OBS-Replay-Buffer-for-IT-Su…/scripts/Invoke-ReplaySave.ps1
2026-03-27 11:16:39 -07:00

62 lines
1.8 KiB
PowerShell

#Requires -Version 5.1
<#
.SYNOPSIS
Sends a SaveReplayBuffer request to OBS via WebSocket v5 (built into OBS 28+).
Returns $true on success, $false on any failure.
.PARAMETER Port
OBS WebSocket port. Defaults to the value in config.psd1.
#>
param(
[int]$Port = 0
)
$ErrorActionPreference = 'SilentlyContinue'
if ($Port -eq 0) {
$config = Import-PowerShellDataFile -Path (Join-Path $PSScriptRoot '..\config.psd1')
$Port = $config.WebSocketPort
}
$ws = $null
$cts = $null
try {
$ws = New-Object System.Net.WebSockets.ClientWebSocket
$cts = New-Object System.Threading.CancellationTokenSource(5000) # 5-second timeout
$uri = [System.Uri]"ws://localhost:$Port"
$ws.ConnectAsync($uri, $cts.Token).Wait()
$buffer = New-Object byte[] 8192
# Receive Hello (opcode 0)
$null = $ws.ReceiveAsync($buffer, $cts.Token).Result
# Send Identify (opcode 1) — no authentication
$identify = [System.Text.Encoding]::UTF8.GetBytes('{"op":1,"d":{"rpcVersion":1}}')
$ws.SendAsync($identify, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $cts.Token).Wait()
# Receive Identified (opcode 2)
$null = $ws.ReceiveAsync($buffer, $cts.Token).Result
# Send SaveReplayBuffer request (opcode 6)
$request = [System.Text.Encoding]::UTF8.GetBytes('{"op":6,"d":{"requestType":"SaveReplayBuffer","requestId":"itrecord-1"}}')
$ws.SendAsync($request, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, $cts.Token).Wait()
# Receive RequestResponse (opcode 7)
$null = $ws.ReceiveAsync($buffer, $cts.Token).Result
$ws.CloseAsync([System.Net.WebSockets.WebSocketCloseStatus]::NormalClosure, 'Done', $cts.Token).Wait()
return $true
}
catch {
return $false
}
finally {
if ($ws) { $ws.Dispose() }
if ($cts) { $cts.Dispose() }
}