@@ -91,7 +91,11 @@ function Get-WebSocket {
91
91
92
92
# The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
93
93
[TimeSpan ]
94
- $TimeOut ,
94
+ $TimeOut ,
95
+
96
+ # The maximum number of messages to receive before closing the WebSocket.
97
+ [long ]
98
+ $Maximum ,
95
99
96
100
# The maximum time to wait for a connection to be established.
97
101
# By default, this is 7 seconds.
@@ -141,6 +145,8 @@ function Get-WebSocket {
141
145
142
146
$webSocketStartTime = $Variable.WebSocketStartTime = [DateTime ]::Now
143
147
$Variable.WebSocket = $ws
148
+
149
+ $MessageCount = [long ]0
144
150
145
151
146
152
while ($true ) {
@@ -149,9 +155,17 @@ function Get-WebSocket {
149
155
$ws.CloseAsync ([Net.WebSockets.WebSocketCloseStatus ]::NormalClosure, ' Timeout' , $CT ).Wait()
150
156
break
151
157
}
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
+
152
165
$Buf = [byte []]::new($BufferSize )
153
166
$Seg = [ArraySegment [byte ]]::new($Buf )
154
167
$null = $ws.ReceiveAsync ($Seg , $CT ).Wait()
168
+ $MessageCount ++
155
169
$JS = $OutputEncoding.GetString ($Buf , 0 , $Buf.Count )
156
170
if ([string ]::IsNullOrWhitespace($JS )) { continue }
157
171
try {
0 commit comments