Skip to content

Commit b1b6f74

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Maximum ( Fixes #22 )
1 parent 3c8b30d commit b1b6f74

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ function Get-WebSocket {
9191

9292
# The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
9393
[TimeSpan]
94-
$TimeOut,
94+
$TimeOut,
95+
96+
# The maximum number of messages to receive before closing the WebSocket.
97+
[long]
98+
$Maximum,
9599

96100
# The maximum time to wait for a connection to be established.
97101
# By default, this is 7 seconds.
@@ -141,6 +145,8 @@ function Get-WebSocket {
141145

142146
$webSocketStartTime = $Variable.WebSocketStartTime = [DateTime]::Now
143147
$Variable.WebSocket = $ws
148+
149+
$MessageCount = [long]0
144150

145151

146152
while ($true) {
@@ -149,9 +155,17 @@ function Get-WebSocket {
149155
$ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, 'Timeout', $CT).Wait()
150156
break
151157
}
158+
159+
if ($Maximum -and $MessageCount -ge $Maximum) {
160+
$ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, 'Maximum messages reached', $CT).Wait()
161+
break
162+
}
163+
164+
152165
$Buf = [byte[]]::new($BufferSize)
153166
$Seg = [ArraySegment[byte]]::new($Buf)
154167
$null = $ws.ReceiveAsync($Seg, $CT).Wait()
168+
$MessageCount++
155169
$JS = $OutputEncoding.GetString($Buf, 0, $Buf.Count)
156170
if ([string]::IsNullOrWhitespace($JS)) { continue }
157171
try {

0 commit comments

Comments
 (0)