Skip to content

Commit

Permalink
feat: Get-WebSocket -TimeOut ( Fixes #23 )
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Nov 28, 2024
1 parent a3fca83 commit 5427701
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Commands/Get-WebSocket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ function Get-WebSocket {
websocket jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Tail |
Foreach-Object {
$in = $_
$spacing = (' ' * (Get-Random -Minimum 0 -Maximum 7))
if ($in.commit.record.text -match "(?>(?:$emojiPattern|\#\w+)") {
Write-Host $matches.0 -NoNewline
$match = $matches.0
Write-Host $spacing,$match,$spacing -NoNewline
}
}
.EXAMPLE
websocket wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Watch |
Where-Object {
$_.commit.record.embed.'$type' -eq 'app.bsky.embed.external'
} |
Foreach-Object {
$_.commit.record.embed.external.uri
}
.EXAMPLE
#>
[CmdletBinding(PositionalBinding=$false)]
param(
Expand Down Expand Up @@ -77,6 +89,10 @@ function Get-WebSocket {
[switch]
$Watch,

# The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
[TimeSpan]
$TimeOut,

# The maximum time to wait for a connection to be established.
# By default, this is 7 seconds.
[TimeSpan]
Expand Down Expand Up @@ -123,11 +139,16 @@ function Get-WebSocket {
$ws = $WebSocket
}

$webSocketStartTime = $Variable.WebSocketStartTime = [DateTime]::Now
$Variable.WebSocket = $ws


while ($true) {
if ($ws.State -ne 'Open') {break }
if ($TimeOut -and ([DateTime]::Now - $webSocketStartTime) -gt $TimeOut) {
$ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, 'Timeout', $CT).Wait()
break
}
$Buf = [byte[]]::new($BufferSize)
$Seg = [ArraySegment[byte]]::new($Buf)
$null = $ws.ReceiveAsync($Seg, $CT).Wait()
Expand Down

0 comments on commit 5427701

Please sign in to comment.