Skip to content

Commit 5427701

Browse files
author
James Brundage
committed
feat: Get-WebSocket -TimeOut ( Fixes #23 )
1 parent a3fca83 commit 5427701

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,22 @@ function Get-WebSocket {
2323
websocket jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Tail |
2424
Foreach-Object {
2525
$in = $_
26+
$spacing = (' ' * (Get-Random -Minimum 0 -Maximum 7))
2627
if ($in.commit.record.text -match "(?>(?:$emojiPattern|\#\w+)") {
27-
Write-Host $matches.0 -NoNewline
28+
$match = $matches.0
29+
Write-Host $spacing,$match,$spacing -NoNewline
2830
}
2931
}
32+
.EXAMPLE
33+
websocket wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Watch |
34+
Where-Object {
35+
$_.commit.record.embed.'$type' -eq 'app.bsky.embed.external'
36+
} |
37+
Foreach-Object {
38+
$_.commit.record.embed.external.uri
39+
}
40+
.EXAMPLE
41+
3042
#>
3143
[CmdletBinding(PositionalBinding=$false)]
3244
param(
@@ -77,6 +89,10 @@ function Get-WebSocket {
7789
[switch]
7890
$Watch,
7991

92+
# The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
93+
[TimeSpan]
94+
$TimeOut,
95+
8096
# The maximum time to wait for a connection to be established.
8197
# By default, this is 7 seconds.
8298
[TimeSpan]
@@ -123,11 +139,16 @@ function Get-WebSocket {
123139
$ws = $WebSocket
124140
}
125141

142+
$webSocketStartTime = $Variable.WebSocketStartTime = [DateTime]::Now
126143
$Variable.WebSocket = $ws
127144

128145

129146
while ($true) {
130147
if ($ws.State -ne 'Open') {break }
148+
if ($TimeOut -and ([DateTime]::Now - $webSocketStartTime) -gt $TimeOut) {
149+
$ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, 'Timeout', $CT).Wait()
150+
break
151+
}
131152
$Buf = [byte[]]::new($BufferSize)
132153
$Seg = [ArraySegment[byte]]::new($Buf)
133154
$null = $ws.ReceiveAsync($Seg, $CT).Wait()

0 commit comments

Comments
 (0)